解析 Backbone.js Handlebars [Object object]

Parse Backbone.js Handlebars [Object object]

对 handlbars.js 来说是个新东西,但我的 #each 一直有效,直到我尝试访问嵌套属性。具体来说 author.name

<!-- Handlebars.js Blog Template -->
<script id="posts-tpl" type="text/x-handlebars-template">
{{#each post}}
<div class="blog-post">
    <h2 class="blog-post-title"><a href="#">{{title}}</a></h2>
      <p class="blog-post-meta">{{author.name}}</p>
    <div>{{{body}}}</div>
</div>
{{/each}}
</script>

Chrome Javascript 控制台显示作者具有属性 'name' 和正确的数据。

Backbone.js 视图似乎也有效:

var PostsView =  Parse.View.extend({
template: Handlebars.compile($('#posts-tpl').html()),
render: function(){ 
    var collection = { post: this.collection.toJSON() };
    this.$el.html(this.template(collection));
}

我在 http://picze.co/blog

有一个完整的例子

如果您查看传递到模板中的内容,就会发现问题所在。

在您的 render 函数中,尝试检查 this.collection.toJSON(); 实际上是什么。

基本上,它看起来像这样:

[
    {
        author: {
            __type: "pointer",
            className: "_User",
            objectId: "JCAjG1AIN0"
        },
        title: "WTFBBQ",
        body: "<p><h3>test</h3> egg on face. This cannot be good. </p>"   
    }
]

看起来 author 缺少 name 属性

编辑

模型中似乎存在 name 属性,但它嵌套为另一个模型,因此当您调用 .toJSON() 时,您将无法获取它。

一种方法是也包括 author 模型:

var collection = { post: this.collection.toJSON() };
for (var i=0; i<collection.post.length; i++) {
    if (collection.post[i].author) {
        collection.post[i].author = this.collection.at(i).get('author').toJSON();
    }
}

我不熟悉 parse.js 所以这可能不是最好的解决方案,但它应该有效