In Backbone.js, why do silent changes trigger change events eventually?

后端 未结 2 849
半阙折子戏
半阙折子戏 2021-02-05 04:29

When I pass {\"silent\":true} while setting an attribute in a Backbone model, why doesn\'t that just suppress the change:attribute event? What is the a

2条回答
  •  梦毁少年i
    2021-02-05 04:43

    In backbone 0.9.2 the set function runs validation before any changes are updated.

      // Run validation.
      if (!this._validate(attrs, options)) return false;
    

    In case of {silent: true} option is passed, the validation code will not be executed.

      if (options.silent || !this.validate) return true;
    

    That means, model.set({value: 100}, {silent: true}); is able to set "invalid" value into model, so attributes are updated, but change event's are not firing.

    It is useful, then you want to update some field and prevent whole mode validation, so if model is not yet completed, the change still propagated to attributes. But you usually you also want the view to show the change, so you have to manually call model.change() or model.trigger('change:value', model, value).

提交回复
热议问题