I am using slugged URLs for cleaner URLs to convert from this:
to this:
The problem is that, if I were to directly access the page via direct slugged url (http://mydomain.com/#/posts/first-awesome-post), I got a blank page instead.
Here is what I have:
App.Router.map(function() {
this.resource('posts', function() {
this.resource('post', { path: ':post_id' });
});
});
App.Post.FIXTURES = [{
id: 1,
name: 'First Awesome Post',
slug: 'first-awesome-post'
}];
App.PostRoute = Em.Route.extend({
model: function(params) {
return App.Post.find(params.post_id);
},
setupController: function(controller, model) {
this._super.apply(this, arguments);
},
serialize: function(model, params) {
return { post_id: model.get('slug')};
},
});
I have tried the following:
- Replaced
params.post_id
inApp.PostRouter
toparams.post_slug
and entered the following URL to my browser http://mydomain.com/#/posts/first-awesome-post; Result: The page CSS and HTML loads but the data of id #1 did not load (i.e. model hook isn't called) - Got rid of serialize function and entered the following URL to my browser http://mydomain.com/#/posts/1; Result: Everything works fine (CSS, HTML, and data got loaded perfectly)
May I know what I have been doing wrong in this case?
P.S. I am currently using EmberJS version 1.0.0-rc.6
If you want to use the slug you should change the model function and make use of findQuery as you are seaching a model by an attribute other than id http://emberjs.com/api/data/classes/DS.Store.html#method_findQuery, but this function returns an RecordArray, you could use the setupController hook for setting the only one object of the array