Backbone fetch中的Ajax在fetch调用退出后完成,因此fetch调用中没有成功/失败事件
Ajax in Backbone fetch finishes after fetch call exits, so no success/fail event in fetch call
在我的Backbone视图初始化中,我调用一个重写的fetch。Fetch检查数据是否在localStorage中,如果没有,则使用ajax提取数据并填充到localStorage中。
但是,ajax调用需要一些时间,所以fetch方法在ajax完成之前返回。因此,原始的fetch调用从未触发成功事件。
如何从内部获取触发成功事件?我还需要传递数据(一旦ajax完成)进行初始化(或者以某种方式返回数据,或者再次调用成功事件fetch()从localStorage中提取数据)
谢谢!
在appview中初始化:
initialize: function() {
var imageList = new ImageList(),
simageel = $(this.el).find('#imagec'),
simageView;
imageList.fetch({
error: function() { console.log(arguments); },
success: function(collection) {
collection.each(function (model) {
simageView = new SImageView({
model: model
});
simageel.html(simageView.render().el);
console.log('Fetch success.'); // Never fires
});
}
});
覆盖集合中的提取:
fetch: function(options) {
if(!localStorage.getItem("image")) {
var self = this;
$.ajax({
url: '[url]' //Could add async but how to trigger render from this function?
}).done(function(response) {
console.log("Fetched");
$.each(response.items, function(i, item) {
self.create(item); // saves model to local storage
});
}).fail(function(response) {
console.log("Failure in fetch");
});
} else {
// fetch from localStorage works fine
return Backbone.Collection.prototype.fetch.call(this, options);
}
}
fetch: function(options) {
if(!localStorage.getItem("image")) {
var self = this;
$.ajax({
url: '[url]' //Could add async but how to trigger render from this function?
}).done(function(response) {
console.log("Fetched");
$.each(response.items, function(i, item) {
self.create(item); // saves model to local storage
});
options.success(self); //call success func
self.trigger('sync', self, response, options); //fire sync event
}).fail(function(response) {
console.log("Failure in fetch");
options.error(responce) //call error func
});
} else {
// fetch from localStorage works fine
return Backbone.Collection.prototype.fetch.call(this, options);
}
}
一旦您开始使用像$.ajax这样的异步函数,就不能为下一个方法使用返回值。您需要调用callback。
当模型/集合完成提取时,原始主干激发sync
事件。所以在你的代码中做同样的事情。并在View构造函数中执行CCD_ 2以运行下一个方法。
initialize: function() {
var imageList = new ImageList(),
simageel = $(this.el).find('#imagec'),
simageView;
this.listenToOnce(imageList,"sync",function(){
console.log("call next method what you want to do after fetching's finished");
});
imageList.fetch({
error: function() { console.log(arguments); },
success: function(collection) {
collection.each(function (model) {
simageView = new SImageView({
model: model
});
simageel.html(simageView.render().el);
console.log('Fetch success.'); // Never fires
});
}
});
相关文章:
- IE11中的第二个调用取消了第一个Fetch API调用
- Backbone fetch中的Ajax在fetch调用退出后完成,因此fetch调用中没有成功/失败事件
- 如何在fetch post调用后管理重定向请求
- 如何判断页面是否是从fetch()调用的
- fetch()不调用其Success和Error回调函数(backbone.js)
- 主干集合在调用 fetch() 时给出“未定义的 Ajax”错误
- 在不调用 fetch 的情况下使用解析
- Backbone JS Model Fetch在Fiddler中返回200,但调用错误方法
- 只需要在主干javascript fetch()调用完成后运行代码
- 为什么'fetch函数'自动调用'解析函数'在backbone.js集合上
- backbone.js collection-重复调用fetch以获取所有“;页面”;来自服务器
- 什么是缓存fetch调用的正确方法
- 每次get时主干调用fetch
- 在fetch api调用时未设置标头
- Babel将新的Request (fetch-api)转换为函数调用,导致Chrome出现错误
- 主干JS:在fetch调用中分配一个模型属性
- 这个fetch调用问题
- 主干-在我的控制器中从一个fetch调用填充多个模型
- 处理redux中fetch调用时的错误
- Backbone.js Collection'的fetch调用出错