I’m diving into the source code of these two awesome works,however,I can’t figure out the implement of them,how can the moment the data of the model update the view immedately changes,
At the very first beginning,I thought it may use something like object.watch() in Gecko,and obviously I can’t find them in both sources
any ideas of the core thought of them
Thanks in advance~
I’m diving into the source code of these two awesome works,however,I can’t figure out
Share
Lets take a look at backbones Event implementation:
Calling bind you store a passed function (callback) under passed key (ev) in the hash
_callbacks.Unbind just delete all or the passed callbacks from the hash.
Trigger will call all functions that stored under the passed key.
This is one of the advantages of JavaScript that you can store functions with its full scope in a variable an call them later. So add the end there is no magic in databinding. Its just a data structure to save function with an key in a hash and call them using the key.
As the Backbone object View, Model, Controller extends the Events object, you can bind/trigger events on all of it. So when a view binds a function on model change, the model call
this.trigger('change')on when ever something was added, removed etc.