真正在Backbone.js的默默更改模型的属性? [英] Truly change a model attribute silently in Backbone.js?

查看:124
本文介绍了真正在Backbone.js的默默更改模型的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,我可以改变模型的属性而没有触发更改事件?如果你通过 {沉默:真正} 现在,下一次的属性被改变,沉默的变化将触发事件。我可以肯定地更改属性没有改变过的事件被触发?

这变化,骨干0.9.2:

  //无声的变化成为挂起的更改。
对(在this._silent VAR attr)使用this._pending [ATTR] =真;//无声的变化引发的。
VAR变化= _.extend({},options.changes,this._silent);
this._silent = {};
对于(的变化VAR ATTR){
    this.trigger('的变化:+ ATTR,对此,this.get(attr)使用,期权);


解决方案

我认为最干净的方式,如果你真的想默认为沉默的(但仍能够做沉默:假)设置将覆盖设置。这应该做到这一点:

  VAR SilentModel = Backbone.Model.extend({    设置:功能(ATTRS,期权){
        选项​​=选项|| {};
        如果(!期权(沉默)){
            options.silent = TRUE;
        }
        返回Backbone.Model.prototype.set.call(这一点,ATTRS,期权);
    }
});

Is there a way I can change attributes on a model without ever firing a change event? If you pass {"silent":true} right now, the next time an attribute is changed, the silent change event will be triggered. Can I change an attribute safely without the change event ever being triggered?

from change, Backbone 0.9.2:

// Silent changes become pending changes.
for (var attr in this._silent) this._pending[attr] = true;

// Silent changes are triggered.
var changes = _.extend({}, options.changes, this._silent);
this._silent = {};
for (var attr in changes) {
    this.trigger('change:' + attr, this, this.get(attr), options);

解决方案

I think the cleanest way if you really want to default to silent (but still be able to do silent:false) sets would be to override set. This should do it:

var SilentModel = Backbone.Model.extend({

    set: function(attrs, options) {
        options = options || {};
        if (!('silent' in options)) {
            options.silent = true;
        }
        return Backbone.Model.prototype.set.call(this, attrs, options);
    }
});

这篇关于真正在Backbone.js的默默更改模型的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆