我正在尝试为 this.model.fetch({}) 发生时设置一个加载屏幕,不确定如何调用它...这是我当前尝试使用事件触发器但它没有触发...
search: function (e) {
console.log('searching...');
var eventL = this.vent;
e.preventDefault();
var that;
that = this.model;
//post instance to the server with the following input fields
this.model.fetch(
{data: {
channel: $('#channel').val(),
week: $('#week').val(),
year: $('#year').val(),
filter: $('#filter').val()
},
attack: this.options.vent.trigger('ok', data),
success: $.proxy(this.storeMusic, this )
});
},
如果可能的话,我也想发回数据,这样我就可以将这些值包含在搜索屏幕中。
最佳答案
如果您试图将状态保存回服务器,您需要使用 save
方法而不是 fetch
。 save
方法(以及 fetch
)接受成功回调(参见 documentation ),您可以使用它来触发隐藏加载屏幕的事件。
是这样的:
var self = this;
// About to save, fire event to indicate loading screen should be displayed
this.options.vent.trigger('saveStarted');
this.model.save({
channel: $('#channel').val(),
week: $('#week').val(),
year: $('#year').val(),
filter: $('#filter').val()
},
{
success: function(model /*, response, options */) {
// Save completed, fire event to indicate loading screen should be hidden
// The model is available as the first parameter of the callback
self.options.vent.trigger('saveCompleted', model);
}
});
关于javascript - 如何在调用模型获取时在 Backbone 中创建加载屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19716653/