移动你结合initialize
将是最好的,但假设你有很好的理由不去,你可以设置一个标志:
initialize: function() {
var _this = this;
this._finish_initializing = _.once(function($options) {
_this.collection.on('reset', function() {
_this.render($options);
});
});
//...
},
render: function($options) {
this._finish_initializing($options);
//...
}
有很多不同的方法来实施国旗,_.once
只是很好地隐藏了国旗检查。你也可以触发事件render
有取消绑定自己的监听器:
initialize: function() {
var finish_initializing = function($options) {
/* your binding goes here ... */
this.off('render', finish_initializing);
};
this.on('render', finish_initializing, this);
},
render: function($options) {
this.trigger('render', $options);
//...
}
这确实是同样的逻辑,只是打扮不同的衣服。您也可以使用明确的标志和render
中的if
或将函数分配给中的this._finish
,该功能将delete this._finish
。
完美的作品,非常感谢= D – Kay