如何在调用模型获取时在骨干中创建加载屏幕?

时间:2013-10-31 21:09:27

标签: javascript backbone.js

我正在尝试设置一个加载屏幕,同时发生了一个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 )
    });
    },

我想尽可能地发回数据,这样我就可以将这些值包含在搜索屏幕中。

1 个答案:

答案 0 :(得分:2)

如果您尝试将状态保存回服务器,则需要save方法而不是fetchsave方法(以及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);
  }
});