所以我不确定最好的方法是什么,目前我有一个看起来像这样的ajax调用
var _this = this;
_.each(this.models, function (model) {
model.fetch({
success: function(model, response) {
if (_.has(model.attributes, "settings")) {
_this.populateSettings(_this);
}
}
在populateSettings方法中,我需要在此Backbone视图中使用某些属性。我不确定如何获取这些属性,除了传递每个特定属性,或传递对此骨干视图的引用,并在populateSettings中,使用该引用来填充视图。有一个更好的方法吗?感谢。
答案 0 :(得分:2)
一种选择是在您的视图中监听sync
上的model
个事件,这样只要model
成功与服务器中的数据同步,您的视图就可以运行{{1} }}
这看起来像这样:
populateSettings
注意第三个参数model.on('sync', this.populateSettings, this)
。传递this
作为第三个参数设置事件处理程序将在其中运行的上下文。来自Backbone.js文档:
要在调用回调时为此提供上下文值,请传递可选的第三个参数:
this
如果您使用了这样的事件监听器,则可以完全删除model.on('change', this.render, this)
选项。您还可以发现success:
或change
事件也很有用。