-
-
Save ModuloM/5246946 to your computer and use it in GitHub Desktop.
Revisions
-
addyosmani revised this gist
Mar 18, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ The Mediator pattern enables communication (mediation) between views using a mediator object.In the latest version of Backbone, the `Backbone` object itself can be used as a mediator without the need of a seperate helper. In the following example, `triggerSomething` in our `ToolbarView` uses the global event-bus on the `Backbone` object to broadcast an application wide event `somethingHappened` with data. -
addyosmani revised this gist
Mar 17, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ var ToolbarView = Backbone.View.extend({ }); ``` In `TableView`, we then subscribe to the global `somethingHappened` event in our view initialization using `Backbone.on()`, executing some behavior whenever we are notified of a change. We are able to similarly unsubscribe in our view destruction using `Backbone.off()`: ```javascript // Second view -
addyosmani revised this gist
Mar 17, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ Using the Mediator pattern is an effective way to avoid tightly coupling your views. In the latest version of Backbone, the `Backbone` object itself can be used as a mediator without the need of a seperate helper. In the following example, `triggerSomething` in our `ToolbarView` uses the global event-bus on the `Backbone` object to broadcast an application wide event `somethingHappened` with data. -
addyosmani revised this gist
Mar 17, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ Using the Mediator pattern is an effective way to avoid tightly coupling your views. In the latest version of Backbone, the `Backbone` object itself can be used as a mediator without the need for any additional code. In the following example, `triggerSomething` in our `ToolbarView` uses the global event-bus on the `Backbone` object to broadcast an application wide event `somethingHappened` with data. -
addyosmani revised this gist
Mar 17, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,11 +19,11 @@ var ToolbarView = Backbone.View.extend({ }); ``` In our second view `TableView`, we subscribe to the global `somethingHappened` event in our view initialization using `Backbone.on()`, executing some behavior whenever we are notified of a change. We are able to similarly unsubscribe in our view destruction using `Backbone.off()`: ```javascript // Second view var TableView = Backbone.View.extend({ initialize: function () { Backbone.on('somethingHappened', onSomething); -
addyosmani revised this gist
Mar 17, 2013 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,6 @@ In the following example, `triggerSomething` in our `ToolbarView` uses the globa ```javascript // First view var ToolbarView = Backbone.View.extend({ className: ".toolbar", events: { "click .button": "triggerSomething" @@ -20,7 +19,7 @@ var ToolbarView = Backbone.View.extend({ }); ``` In our second view, we subscribe to the global `somethingHappened` event in our view initialization using `Backbone.on()`, executing some behavior whenever we are notified of a change. We are able to similarly unsubscribe in our view destruction using `Backbone.off()`: ```javascript // Second view -
addyosmani renamed this gist
Mar 17, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
addyosmani created this gist
Mar 17, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ Using the Mediator pattern is a good way to avoid tightly coupling your views. In the latest version of Backbone, the `Backbone` object itself can be used as a mediator without the need for any additional code. In the following example, `triggerSomething` in our `ToolbarView` uses the global event-bus on the `Backbone` object to broadcast an application wide event `somethingHappened` with data. ```javascript // First view var ToolbarView = Backbone.View.extend({ className: ".toolbar", events: { "click .button": "triggerSomething" }, triggerSomething: function(){ Backbone.trigger('somethingHappened', { id: 2 }); }, render: function() { ... } }); ``` In our second view, we subscribe to the global `somethingHappened` event in our view initialization using `Backbone.on()`, executing some behavior whenever we are notified of a change. We are able to similarly unsubscribe in our view destruction using ` ```javascript // Second view var tableView = Backbone.View.extend({ initialize: function () { Backbone.on('somethingHappened', onSomething); }, render: function(){ // ... }, destroy: function () { Backbone.off('somethingHappened', onSomething); }, // Action when something happens. onSomething: function (data) { console.log('Received:' + data.id); } }); ``` The more recent `listenTo` method may also be used for the same purpose.