Skip to content

Instantly share code, notes, and snippets.

@deepflame
Last active August 29, 2015 14:04
Show Gist options
  • Save deepflame/29ae7184b42b17108131 to your computer and use it in GitHub Desktop.
Save deepflame/29ae7184b42b17108131 to your computer and use it in GitHub Desktop.
EmberJS: example for nesting routes / resources
body {
padding: 15px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script type="text/x-handlebars" data-template-name="posts">
<h2>Posts</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="posts/index">
{{#each}}
{{#link-to 'post' this}}{{title}}{{/link-to}}
{{/each}}
</script>
<script type="text/x-handlebars" data-template-name="post">
<h4>{{title}}</h4>
<p>{{body}}</p>
{{link-to 'Back' 'posts'}}
</script>
<script src="http://emberjs.com.s3.amazonaws.com/getting-started/jquery.min.js"></script>
<script src="http://emberjs.com.s3.amazonaws.com/getting-started/handlebars.js"></script>
<script src="http://builds.emberjs.com/tags/v1.6.1/ember.min.js"></script>
<script src="http://builds.emberjs.com/tags/v1.0.0-beta.8/ember-data.min.js"></script>
</body>
</html>
window.App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter;
// boilerplate for ember-data
App.Post = DS.Model.extend({
title: DS.attr('string'),
body: DS.attr('string')
});
App.Post.FIXTURES = [
{ id: 1, title: 'First', body: 'foo foo' },
{ id: 2, title: 'Second', body: 'bar bar'}
];
// here is the interesting part
App.Router.map(function () {
this.resource('posts', function () {
this.resource('post', { path: ':post_id'});
});
// not the same (does not create posts/index route)
//this.resource('posts');
//this.resource('post', { path: '/posts/:post_id'});
});
App.IndexRoute = Ember.Route.extend({
redirect: function () {
this.transitionTo('posts');
}
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment