Skip to content

Instantly share code, notes, and snippets.

@ModuloM
Forked from addyosmani/mediatorExample.md
Created March 26, 2013 16:42
Show Gist options
  • Select an option

  • Save ModuloM/5246946 to your computer and use it in GitHub Desktop.

Select an option

Save ModuloM/5246946 to your computer and use it in GitHub Desktop.

Revisions

  1. @addyosmani addyosmani revised this gist Mar 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mediatorExample.md
    Original 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.
    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.

  2. @addyosmani addyosmani revised this gist Mar 17, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mediatorExample.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ 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()`:
    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
  3. @addyosmani addyosmani revised this gist Mar 17, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mediatorExample.md
    Original 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.
    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.

  4. @addyosmani addyosmani revised this gist Mar 17, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mediatorExample.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    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.
    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.

  5. @addyosmani addyosmani revised this gist Mar 17, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mediatorExample.md
    Original file line number Diff line number Diff line change
    @@ -19,11 +19,11 @@ 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()`:
    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({
    var TableView = Backbone.View.extend({

    initialize: function () {
    Backbone.on('somethingHappened', onSomething);
  6. @addyosmani addyosmani revised this gist Mar 17, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions mediatorExample.md
    Original 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 `
    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
  7. @addyosmani addyosmani renamed this gist Mar 17, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @addyosmani addyosmani created this gist Mar 17, 2013.
    48 changes: 48 additions & 0 deletions mediatorExample.js
    Original 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.