Skip to content

Commit

Permalink
Merge pull request tbranyen#59 from liamks/master
Browse files Browse the repository at this point in the history
Added documentation for app wide events
  • Loading branch information
tbranyen committed Feb 17, 2012
2 parents 09eaabf + e2e52f1 commit 3e1f396
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/templates/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ <h1>Contents</h1>
<li><a data-bypass href="#namespace">Setting your namespace</a>
<li><a data-bypass href="#modules">Creating a module</a>
<li><a data-bypass href="#templates">Working with templates</a>
<li><a data-bypass href="#events">Working with Application Wide Events</a>
<li><a data-bypass href="#plugins">Working with libraries and plugins</a>
</ul>
<li><a data-bypass href="#custom-build">Using the build tool</a>
Expand Down Expand Up @@ -265,6 +266,41 @@ <h3 id="templates">Working with templates</h3>
</code></pre>
</p>

<h3 id="events">Working with Application Wide Events</h3>
<p>Application wide events provide a convenient way for modules to communicate with each other. <code>namespace.app</code> references a copy of the Backbone.Events object. <a href="http://documentcloud.github.com/backbone/#Events">More information on Backbone Events</a></p>

<p>
To bind a callback function to an event:

<pre><code>
//binding an annonymous function to the event "all" with a context of <code>this</code>.
namespace.app.on("all", function(){...}, this);
</code></pre>
</p>

<p>
To remove a callback function (or many) from an event:

<pre><code>
// Removes just the doSomething callback.
namespace.app.off("change", doSomething);

// Removes all "change" events.
namespace.app.off("change");

// Removes all events from the namespace.app object.
namespace.app.off();
</code></pre>
</p>

<p>
To trigger the "change" event:

<pre><code>
namespace.app.trigger("change", [*args]);
</code></pre>
</p>

<h3 id="plugins">Working with libraries and plugins</h3>
<p>Libraries and plugins are easily added to the application, by placing them inside the <code>assets/js/libs/</code> directory.
If you have many plugins in your application, it may make sense to create a separate folder such as <code>assets/js/plugins/</code>
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ MyModule.Model = Backbone.Model.extend({ /* ... */ });
MyModule.Router = Backbone.Router.extend({ /* ... */ });
```

## Events ##

Application wide events provide a convenient way for modules to communicate with each other. `namespace.app` references a copy of the Backbone.Events object, providing access to `.on()`, `.off()`, and `.trigger()`, that are documented in [Backbone.js Events](http://documentcloud.github.com/backbone/#Events)

For example, to add a callback to the "all" event:

```javascript
namespace.app.on("all", function(){}, this);
```

## HTML5 History and Hash Based Navigation ##

Out the box Backbone Boilerplate enables `pushState`. It also supplies a script
Expand Down

0 comments on commit 3e1f396

Please sign in to comment.