Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
Created July 22, 2014 19:43
Show Gist options
  • Save jamiebuilds/7814846a9d0e98232573 to your computer and use it in GitHub Desktop.
Save jamiebuilds/7814846a9d0e98232573 to your computer and use it in GitHub Desktop.
// Create our Application
var app = new Marionette.Application();
// Attach a router to it
app.router = new Marionette.Router({
channelName: 'myRouter',
states: {
'profile': {
url: 'profile',
view: profileView,
controller: ProfileController
},
'profile.settings': {
url: 'profile/settings',
region: '.some-selector'
}
}
});
app.router.addState({
'whatever': {
url: 'whatever',
regions: {
'myRegion': {
selector: '.my-selector',
view: MyView
}
'yourRegion': {
selector: '.your-selector',
view: YourView
}
}
},
'whatever.popcorn': {
controller: PopcornController
}
});
var PopcornController = Marionette.Controller.extend({
initialize: function(options) {
// All of the regions
this.regions;
// All of the views
this.views;
// The URL parts
this.urlParts;
// The query params
this.params;
}
});
// Attach this information directly to the Controller if you choose
_.extend(PopcornController, {
view: MyView,
region: '.some-region'
});
// Create our Application
var app = new Marionette.Application();
// Attach a router to it
app.router = new Marionette.Router();
// Configure our router
app.router
// A router is a state machine. So we configure our states, by name.
.state('profile', {
// Matches the profile URL
url: 'profile',
// The view to render in our region
view: profileView,
// Options to pass to the view. Also accepts a function that returns the options
viewOptions: myViewOptions,
// The controller for this state.
controller: profileCtrl
})
// Nest views with the dot syntax
.state('profile.settings', {
url: 'profile/settings',
view: settingsView,
// Specify a custom selector for this view instead of the default.
region: '.whatever-selector',
controller: settingsCtrl
});
// You can also configure it across different files. The use case for this is that
// each 'module' sets up its own routes.
app.router.state('friends', { ... });
/*
* Let's examine one of those controllers...
*/
var profileCtrl = Marionette.Controller.extend({
// A new controller is instantiated everytime you navigate to the route
initialize: function(options) {
// An array of the views for this state
this.views = options.views;
// The model that is passed to each view. Add more data
// to it here if you want to
this.model = options.model;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment