Skip to content

Commit

Permalink
Applying sub route changes to the branch
Browse files Browse the repository at this point in the history
Signed-off-by: J. Renée Beach <splendidnoise@gmail.com>
  • Loading branch information
jessebeach committed Apr 29, 2012
1 parent 0644a53 commit 9220deb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
14 changes: 12 additions & 2 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ require([
"modules/example"
],

function(namespace, $, Backbone, Example) {
function(namespace, $, Backbone) {

// Get the list of overloaded arguments.
// These will be the modules defined in require. Modules should not be passed
// as explicit arguments of the callee function.
var Modules = Array.prototype.slice.call(arguments, arguments.callee.length) || [];

// Defining the application router, you can attach sub routers here.
var Router = Backbone.Router.extend({
Expand All @@ -18,6 +23,11 @@ function(namespace, $, Backbone, Example) {
":hash": "index"
},

// Add module routes and route callbacks to the Router.
initialize: function(Modules) {
namespace.mergeSubroutes.call(this, Modules);
},

index: function(hash) {
var route = this;
var tutorial = new Example.Views.Tutorial();
Expand Down Expand Up @@ -50,7 +60,7 @@ function(namespace, $, Backbone, Example) {
$(function() {
// Define your master router on the application namespace and trigger all
// navigation from this instance.
app.router = new Router();
app.router = new Router(Modules);

// Trigger the initial route and enable HTML5 History API support
Backbone.history.start({ pushState: true });
Expand Down
2 changes: 1 addition & 1 deletion app/modules/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ define([
function(namespace, Backbone) {

// Create a new module
var Example = namespace.module();
var Example = namespace.module({name: 'example'});

// Example extendings
Example.Model = Backbone.Model.extend({ /* ... */ });
Expand Down
18 changes: 17 additions & 1 deletion app/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ function($, _, Backbone) {
},

// Keep active application instances namespaced under an app object.
app: _.extend({}, Backbone.Events)
app: _.extend({}, Backbone.Events),

// Add module routes and route callbacks to the app.
mergeSubroutes: function(Modules) {
for (var i = 0; i < Modules.length; i++) {
var m = Modules[i];
// Proceed if the module extends Backbone.Router.
if (m.Router) {
var subrouter = new m.Router();
if (subrouter.routes) {
// Extend the app's routes and route callbacks
// with the submodule's routes and route callbacks.
$.extend(true, this, subrouter);
}
}
}
}
};
});

0 comments on commit 9220deb

Please sign in to comment.