Sometimes is useful to extend a class with a mixin rather than keeping deriving a subclass to add functionalities (Mixin). A mixin could be useful also to keep your class thin.
In Backbone this is quite easy
Then it may happens that we need to initialise the mixin, for example to setup some variables (in this example the initial value of the property is a simple number but this could lead to some problem in case the property is a variable passed by reference). The temptation is to call the initialize method of the mixin right in the extended class
But this is not a good practice since the extended class now depends on the mixin class. One big advantage of the mixin is that we can add the some methods to different classes without having to derive a subclass from each of them, a kind of simple multiple inheritance.
This works if the mixin is loosely coupled and my general rule of thumb is “if a method refers to something in the mixin, it must be in the mixin”.
We can solve it with a little bit of conventions and meta programming
Basically the initialize method of the Backbone class to be extended searches for methods like initialize* and execute them. With this convention we saved our loosely coupling (if we remove, add or change our mixin, nothing changes in the class to be extended).
Callbacks are a nice way of Iversion of control, but are even more powerful with deferred objects.
Basically callbacks are a way to pass back the control from a called function, the deferred/promise just do the opposite: it returns the control the called function.
Consider this scenario: a table widget, with the classic delete/modify buttons next to each row. Since the table widget is a generic component, we want to handle the delete operation on the model outside the widget, and we don’t event want to extend the generic object to do that, we want to user a callback:
In the example above: the callback first calls the backend to remove the record and then removes the row from the table (I mean the DOM). In order to do this: it needs to know something about the table widget (in this case, the method “remove_row”).
But what if we could pass the ball back to the widget? Let’s rewrite it with a deferred object:
It’s more linear but, most important, the callback knows nothing about the remove_that_damn_row_from_the_dom method of the widget, it just passed back the control, it’s like saying “I’m done, it’s your turn”.
More separation, less documentation to read, easier to implement, less errors.
On the widget side, the callback should be treated this way
Eventbroadcast it’s a small library to improve the functionality of bind()/trigger() methods.
This plugin extends jQuery with the method $.trigger(‘
The main problem with the classic jQuery $(‘
With the Publish/Subscribe pattern it’s easy to decouple the code in charge to create the event from the code that receives it: the event emitter doesn’t know (and doesn’t care), at runtime, who is listening to the event (speaking with the words of academics, it’s not aware of the topology of the elements).
The more the code is decoupled, the more is maintanable.