javascript 保存 Backbone 模型时如何触发事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8883653/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:52:36  来源:igfitidea点击:

How to trigger an event when a Backbone model is saved?

javascripteventsbindingbackbone.js

提问by Bart Jacobs

If I have done my homework correctly, I have come to learn that Backbone does not have built-in saveevent that is triggered when a model is saved using the model's save method (even though there is a destroyevent).

如果我正确地完成了作业,我就会了解到 Backbone 没有内置的保存事件,当使用模型的保存方法保存模型时会触发该事件(即使有销毁事件)。

I have also learned that Backbone has a nifty mechanism for creating custom events using the Backbone.Events object. Using the latter works, but I have the impression that it is not fine-grained enough for my needs.

我还了解到 Backbone 有一个使用 Backbone.Events 对象创建自定义事件的漂亮机制。使用后者是有效的,但我的印象是它不够细粒度满足我的需求。

My setup is as follows. I have a table (view) built up of rows (views) with each row having a reference to a model. When the model is saved, I'd like to update/render the row to reflect the changes.

我的设置如下。我有一个由行(视图)组成的表(视图),每一行都有一个对模型的引用。保存模型后,我想更新/呈现行以反映更改。

How does one go about creating a saveevent that is triggered when a model is saved so that the table row (view) that has a reference to that model is updated?

如何创建保存模型时触发的保存事件,以便更新引用该模型的表行(视图)?

In other words, I'd like to be able to do the following:

换句话说,我希望能够做到以下几点:

this.model.bind('save', this.render);

回答by Paul

Just 3 days ago, a commitwas made to Backbone that triggers a syncevent when the model is successfully saved. This commit hasn't been release yet, though, so you will need to download the source code from the github account if you want to use it.

就在 3 天前,对 Backbone 进行了提交sync当模型成功保存时触发事件。但是,此提交尚未发布,因此如果您想使用它,则需要从 github 帐户下载源代码。

View = Backbone.View.extend({
  initialize: function() {
    _.bindAll(this, 'onModelSaved');
    this.model.bind('sync', onSuccessCallback);
  },

  onModelSaved: function(model, response, options) {
    //perform your after save logic
  }
});

回答by Venkat Kotra

As of Backbone.js 1.0.0 you have syncevent that is triggered if the model is saved successfully.

从 Backbone.js 1.0.0 开始sync,如果模型保存成功,就会触发事件。

this.listenTo(this.model,'sync', this.render);

Note that, the change:attributeis fired first for relevant attributes if there is change in value of the attribute, followed by the changeevent then followed by the syncevent. syncevent is fired irrespective of the change in model. It is to denote that the model is now in sync with server values.

请注意,change:attribute如果属性值发生变化,则首先为相关属性触发,然后是change事件,然后是sync事件。 sync无论模型如何变化,都会触发事件。表示模型现在与服务器值同步。

Also these event fire only if the values are valid. i.e models.validateshould not return any errors for these values got from the server.

此外,仅当值有效时才会触发这些事件。即models.validate不应该为从服务器获得的这些值返回任何错误。