无法迭代骨干集合,可能是一个获取/绑定问题

时间:2012-06-08 11:25:40

标签: backbone.js handlebars.js backbone.js-collections

我正在抓取一个用户朋友列表到一个集合中,我想迭代这些朋友,以便用户可以选择一个朋友并在以后做一堆事情。

在我的initalize中我有

friends = new MyApp.Collections.UserFriends();
friends.fetch({ data: $.param({ search: this.model.attributes.user_id}),
            success: function(){

            },
            error: function(){
                alert('error getting friends');
            }
         });


         friends.bind('fetched', this.render(),this);

然后在我的渲染中我有

console.log(friends);
    console.log(HandlebarsTemplates['friends/friendsResults'](friends);
    $(this.el).html(HandlebarsTemplates['friends/friendsResults'](friends);

我的把手是

ok, I have the right handlebar template
{{#each models}}
<li>   {{attributes.username}}</li>
{{/each}}

控制台中的朋友集合是,所以我可以看到模型在那里。

byCid: Object
_byId: Object
_callbacks: Object
length: 39
models: Array[39]
__proto__: q

我担心的是问题是在收集之前触发了渲染,但这就是我认为friends.bind('fetched',this.render(), this)应该解决的问题。

1 个答案:

答案 0 :(得分:2)

Backbone中没有'抓取'事件,尝试使用'重置',http://backbonejs.org/#FAQ-events