Backbone.js的未捕获的Ref​​erenceError [英] Backbone.js Uncaught ReferenceError

查看:245
本文介绍了Backbone.js的未捕获的Ref​​erenceError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进出口新的骨干力量,试图从话语preSS JSON插件获取JSON和渲染成一个模板,但通过帖子循环时,我得到以下错误的未捕获的Ref​​erenceError:帖子没有定义任何帮助AP preciated。谢谢...

 的jQuery(函数($){
    VAR邮政= Backbone.Model.extend();
    VAR帖子= Backbone.Collection.extend({
        型号:邮政,
        网址:'/ API / get_post / JSON = get_recent_posts?,
        解析:功能(RESP){
            的console.log(上岗,RESP);
            返回RESP;
        }
    });
    VAR的ListView = Backbone.View.extend({
        EL:#内容,
        模板:_.template($(#后的模板)HTML()。)
        初始化:功能(){
            this.model.bind(复位,this.render,这一点);
        },
        渲染:功能(){
            $(this.el)的.html(this.template(this.model.toJSON()));
            返回此;
        }
    });
    VAR AppRouter = Backbone.Router.extend({
        路线:{
            !/档案:档案
        },
        档案:功能(){
            this.postList =新帖();
            this.postListView =新的ListView({
                型号:this.postList
            });
            this.postList.fetch();
            this.postListView.render();
        }
    });
    VAR应用=新AppRouter();
    Backbone.history.start();
});

模板

 <脚本ID =模板后类型=文/模板>
    < UL>
      <%_.each(帖子,功能(POST){%GT;
        <李ID =<%= post.id%GT;>
          &所述; A HREF =&下;%= post.url%​​gt;中与GT;&下;%= post.thumbnail%GT;&下; / A>
        < /李>
      &所述;%}); %GT;
    < / UL>
< / SCRIPT>

的Json

  {
    状态:OK,
    伯爵:1,
    count_total:1,
    页:1,
    上岗:[{
        ID:4,
        类型:后
        塞:试岗,
        URL:HTTP:\\ / \\ /本地主机:8888 \\ / 2013 \\ / 04 \\ /测试后\\ /
        状态:发布,
        头衔:测试邮报,
        title_plain:测试邮报,
        内容:,
        摘录:,
        日期:2013年4月17日15时12分21秒,
        修改:2013年4月19日14时13分00秒,
        类别:[],
        标签:[],
        作者:{
            标识:1,
            弹头:管理员,
            名:管理员,
            名字: ,
            姓: ,
            昵称:管理员,
            URL:,
            说明:
        },
        注释: [],
        COMMENT_COUNT:0,
        comment_status:关闭,
        缩略图:HTTP:\\ / \\ /本地主机:8888 \\ /可湿性粉剂内容\\ /上传\\ / 2013 \\ / 04 \\ /test-image-150x150.jpg
    }]
}


解决方案

调用收藏#的toJSON 将返回的阵列。因此,没有任何帖子键。尝试使用 $(this.el)的.html(this.template(this.model.toJSON()[0])); 因为你只需要在您的收藏一个模型(这是奇怪)。

您可能要分析在解析方法您回应返回 resp.posts 让您的收藏有更多的这意味着,实际上创建一个发表平均每个职位的模式。

Im new to backbone, trying to fetch json from the Wordpress json plugin and render into a template, but when looping through posts I get the following error Uncaught ReferenceError: posts is not defined Any help appreciated. Thanks...

   jQuery(function($) {
    var Post = Backbone.Model.extend();
    var Posts = Backbone.Collection.extend({
        model: Post,
        url: '/api/get_post/?json=get_recent_posts',
        parse: function(resp) {
            console.log("posts", resp);
            return resp;
        }
    });
    var listView = Backbone.View.extend({
        el: '#content',
        template: _.template($("#post-template").html()),
        initialize: function() {
            this.model.bind("reset", this.render, this);
        },
        render: function() {
            $(this.el).html(this.template(this.model.toJSON()));
            return this;
        }
    });
    var AppRouter = Backbone.Router.extend({
        routes: {
            "!/archive": "archive"
        },
        archive: function() {
            this.postList = new Posts();
            this.postListView = new listView({
                model: this.postList
            });
            this.postList.fetch();
            this.postListView.render();
        }
    });
    var app = new AppRouter();
    Backbone.history.start();
});

Template

<script id="post-template" type="text/template">
    <ul>
      <% _.each(posts, function(post) { %>
        <li id="<%= post.id %>">
          <a href="<%= post.url %>"><%= post.thumbnail %></a>
        </li>
      <% }); %>
    </ul>
</script>

Json

{
    "status": "ok",
    "count": 1,
    "count_total": 1,
    "pages": 1,
    "posts": [{
        "id": 4,
        "type": "post",
        "slug": "test-post",
        "url": "http:\/\/localhost:8888\/2013\/04\/test-post\/",
        "status": "publish",
        "title": "Test Post",
        "title_plain": "Test Post",
        "content": "",
        "excerpt": "",
        "date": "2013-04-17 15:12:21",
        "modified": "2013-04-19 14:13:00",
        "categories": [],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "admin",
            "name": "admin",
            "first_name": "",
            "last_name": "",
            "nickname": "admin",
            "url": "",
            "description": ""
        },
        "comments": [],
        "comment_count": 0,
        "comment_status": "closed",
        "thumbnail": "http:\/\/localhost:8888\/wp-content\/uploads\/2013\/04\/test-image-150x150.jpg"
    }]
}

解决方案

Calling Collection#toJSON will return an array. Therefore there is no posts key. Try using $(this.el).html(this.template(this.model.toJSON()[0])); as you only have one model in your collection (which is weird).

You may want to parse your response in the parse methods to return resp.posts so your collection has more meaning and actually creates one Post model per post.

这篇关于Backbone.js的未捕获的Ref​​erenceError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆