我已经定义了一个视图绑定到一个集合。
1)当我初始化视图时,会执行一个对集合的获取调用。
2) 如果我查看 get 请求的结果,则可以
3) 我想触发此更改(在 myCollection 中)以调用渲染函数,但它不起作用。
这是我的代码的一部分:
define([
"MyCollection",
"!text/template"
], function (myCollection, myTemplate) {
var myView = Backbone.View.extend({
initialize: function ()
{
myCollection.bind('change', this.render); // why the render is not called when
// I perform the fetch even if the data is correctly loaded?
myCollection.fetch(); // it works
},
render: function ()
{
// some code
this.$el <-- undefined
}
});
return myView;
});
如果我使用
1)
initialize: function ()
{
myCollection.bind('reset', this.render); // this.$el <-- undefined in render call, Why?
myCollection.fetch(); // it works
}
2)
myCollection.fetch({
success: function () {
that.render(); // <-- here the $el is defined and it works, Why myCollection.bind('reset', this.render); not??
// it seems to me that when I use reset the render function is called before the this.$el is defined
}
});