Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 advantage of firing that event the next time an attribute is changed?

Update

Backbone 0.9.10 changed the behavior of passing { "silent": true }. From the changelog:

Passing {silent:true} on change will no longer delay individual "change:attr" events, instead they are silenced entirely.

Browse the changelog here

like image 582
stinkycheeseman Avatar asked Apr 05 '12 14:04

stinkycheeseman


People also ask

How do I trigger an event in Backbone JS?

js trigger Event is used to invoke or callback the function for the given event or a space-delimited list of events. The subsequent arguments will be passed along to the event callbacks in order to trigger it. Parameter Values: event: It is used to bind an object with an event.

How does Backbone js work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

Why use Backbone js?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.

What is Backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).


1 Answers

This has confused me for some time as well.

The reason is that {silent:true} does not mean "Do everything as normal, but just don't trigger the event".

From various comments and answers by @jashkenas, what it seems to mean is "simply change the attribute value (and add it to the 'changedAttributes' hash), but defer all other "change-related" activities until later".

'silent' doesn't prevent the change event for that/those properties, it simply queues up the 'announcement' until the next change event is triggered.

So, its probably better named something like defer.

Relevant information:

https://github.com/documentcloud/backbone/pull/850

the point of a "silent" change is that it isn't considered a change from the models point of view. Later, when the change actually occurs, you get the full difference all at once.

https://github.com/documentcloud/backbone/issues/870

Because the point of silent changes is that you're allowed to twiddle with the internal state of your model, temporarily, without actually making a change. Later, when the attribute actually changes, the validation runs and events are emitted. It wouldn't make sense to emit an error event when making a silent change.

Update 4/7/2013

Note: I have not tested this to confirm behavior, this is just based on my reading of the release notes...

As of Backbone 0.9.10, the behavior described above has changed. In that version (and newer), silent:true suppresses the change:attr events entirely - not just delays them.

http://backbonejs.org/#changelog

like image 129
Edward M Smith Avatar answered Sep 19 '22 17:09

Edward M Smith