javascript 如何知道 Backbone model.fetch() 何时完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14918280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know when a Backbone model.fetch() completes?
提问by user1031947
I bind on the change event of my backbone models like this.
我像这样绑定了我的主干模型的更改事件。
this.model.on( "change", this.render, this );
Sometimes I want to fetch the latest version of the model and forcibly render the view. So I do this
有时我想获取最新版本的模型并强行渲染视图。所以我这样做
this.model.fetch();
Unfortunately model.fetch() only fires the change event if the new data is different from what was previously stored in the model.
不幸的是,如果新数据与之前存储在模型中的数据不同,model.fetch() 只会触发更改事件。
How can I always trigger a this.render callback when fetch completes, whether it triggers a change event or not?
如何在 fetch 完成时始终触发 this.render 回调,无论它是否触发更改事件?
Thanks (in advance) for your help
在此先感谢您的帮助
回答by Ben
You can use the $.ajax
success callback, but you can also just listen for the Backbone sync
and error
events on the model. sync
fires after a successful call to the server, error
fires after a failed call to the server.
您可以使用$.ajax
成功回调,但您也可以只侦听模型上的Backbonesync
和error
事件。sync
在成功调用服务器后error
触发,在调用服务器失败后触发。
this.model.on('sync', this.render, this);
this.model.on('error', this.handleError, this);
回答by JayC
The fetch
method can optionally accept has success and error callbacks; the simplest solution is to put you view's render
in the success callback. You could also probably use the returned jqXHR promise, but if there's ever a case where the AJAX would be successful (per jQuery) but model initialization fails, that usage could be problematic.
该fetch
方法可以选择接受具有成功和错误的回调;最简单的解决方案是将您的视图放入render
成功回调中。您也可以使用返回的 jqXHR 承诺,但如果出现 AJAX 成功(根据 jQuery)但模型初始化失败的情况,则该用法可能会出现问题。
回答by Hayk Aghabekyan
I don't know what is your code structure, however if your are fetching your model inside your view, you can use something like this
我不知道你的代码结构是什么,但是如果你在你的视图中获取你的模型,你可以使用这样的东西
var that = this;
this.model.fetch().done(function () {
that.render();
});
else, if your are fetching your model outside your view, you can pass your promise to your view and make something similar
否则,如果您在视图之外获取模型,则可以将您的承诺传递给您的视图并进行类似的操作
var promise = model.fetch();
// other code here
var view = new View({
model: model,
promise: promise
});
and inside your view, for example in initialize
在您的视图中,例如在初始化中
View = Backbone.View.extend({
initialize: function(){
this.options.promise.done(function () {
// your code here
});
}
});
回答by Andrew Fedyk
How about this solution:
这个解决方案怎么样:
// emit fetch:error, fetch:success, fetch:complete, and fetch:start events
fetch: function(options) {
var _this = this;
options = options || {};
var error = options.error;
var success = options.success;
var complete = options.complete;
options.error = function(xhr, textStatus, errorThrown) {
_this.trigger('fetch:error');
if (error) error(xhr, textStatus, errorThrown);
};
options.success = function(resp) {
_this.trigger('fetch:success');
if (success) success.call(options.context, resp);
};
options.complete = function() {
_this.trigger('fetch:complete');
if (complete) complete();
};
_this.trigger('fetch:start');
return Backbone.Model.prototype.fetch.call(this, options);
}
Link to gist https://gist.github.com/fedyk/23761ce1236c5673fb84