Created
May 11, 2014 20:07
-
-
Save givanse/3d54793d909a0b19029c to your computer and use it in GitHub Desktop.
One route handles two models/controllers/templates and renders in a single template with two {{outlet}}s.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>AB</title> | |
</head> | |
<body> | |
<script type="text/x-handlebars"> | |
One route handles 2 models as 1, with 2 templates. | |
<br><br> | |
path: <b>{{currentPath}}</b><br> | |
{{outlet a}} | |
{{outlet b}} | |
</script> | |
<script type="text/x-handlebars" id="a"> | |
{{model}} | |
</script> | |
<script type="text/x-handlebars" id="b"> | |
{{model}} | |
</script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.4/handlebars.min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/ember.js/1.0.0-rc.6/ember.min.js"></script> | |
</body> |
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
// http://stackoverflow.com/a/17791094/7852 | |
App = Ember.Application.create(); | |
App.Router.map(function() { | |
// This route has 2 dynamic segments | |
this.resource("ab", { path: "/:a/:b" }); | |
}); | |
App.IndexRoute = Ember.Route.extend({ | |
redirect: function() { | |
// At the entry point, encapsulate the 2 models in the context object, | |
// and transition to the route with dual dynamic segments | |
this.transitionTo('ab', {a: 3, b:4}); | |
} | |
}); | |
App.AbRoute = Ember.Route.extend({ | |
model: function(params) { | |
// The model is {a, b} directly | |
return params; | |
}, | |
renderTemplate: function(){ | |
// Render in the named outlet using the right controller | |
this.render('a', { | |
into: 'application', | |
outlet: 'a', | |
controller: 'a'}); | |
this.render('b', { | |
into: 'application', | |
outlet: 'b', | |
controller: 'b'}); | |
}, | |
serialize: function(model) { | |
return { | |
a: model.a, | |
b: model.b | |
}; | |
}, | |
setupController: function(controller, model) { | |
// Setup each controller with its own model | |
this.controllerFor('a').set('model', model.a); | |
this.controllerFor('b').set('model', model.b); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment