Created
January 4, 2013 22:41
-
-
Save mxriverlynn/4458103 to your computer and use it in GitHub Desktop.
using Marionette.Controller as a "mediator" to coordinate multiple views and/or other objects
This file contains 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 characters
// see this article: http://lostechies.com/derickbailey/2012/05/10/modeling-explicit-workflow-with-code-in-javascript-and-backbone-apps/ | |
// replace the object literal in that example with a Marionette.Controller | |
// | |
// an example: | |
var SomeProcess = Marionette.Controller.extend({ | |
initialize: function(options){ | |
this.region = options.region; | |
this.model = options.model; | |
}, | |
show: function(){ | |
var this.layout = this.getLayout(this.model); | |
this.region.show(this.layout); | |
}, | |
onClose: function(){ | |
if (this.layout){ | |
this.layout.close(); | |
} | |
}, | |
getLayout: function(model){ | |
var layout = new MyLayout({ | |
model: model | |
}); | |
this.listenTo(layout, "render", function(){ | |
this.showMenu(model, layout.menuRegion); | |
this.showContent(model.children, layout.contentRegion); | |
}); | |
}, | |
showMenu: function(model, region){ | |
var menu = new MyMenu({ | |
model: model | |
}); | |
this.listenTo(menu, "click:item", this.changeContent); | |
region.show(menu); | |
}, | |
showContent: function(collection, region){ | |
var content = new MyContent({ | |
collection: collection | |
}); | |
this.listenTo(content, "some:event", this.doMoreStuff); | |
region.show(content); | |
}, | |
// ... changeContent & doMoreStuff functions go here | |
}); | |
// use of this object | |
// ------------------ | |
var process = new SomeProcess({ | |
region: MyApp.someRegion, | |
model: someModel | |
}); | |
// calling the `show` method | |
// that we added to the object | |
process.show(); | |
// some time later when we're | |
// done with this process, | |
// close it | |
process.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment