I’m coding a conceptual blog app using FixtureAdapter, in which I have Tag
and Post
models linked through a many to many relationship as follows:
App.Tag = DS.Model.extend({
name: DS.attr('string'),
posts: DS.hasMany('App.Post', { embedded: true})
});
App.Post = DS.Model.extend({
// other properties
tags: DS.hasMany('App.Tag',{
embedded: true
}),
// other properties
});
Then in TagRoute#serialize
I’m doing this
serialize: function(model) {
console.log(model); // logs an object with the correct data
console.log(model.get('name')); // logs undefined
console.log(model.get('id')); // logs the correct id
var i = model.get('id');
var n = model.get('name');
return {
tag_id: i,
tag_name: n
}
}
If I go the a post, I can see my tag links, but I noticed it’s linking to ~/#/tags/undefined
, however, if I open the console, I can see that the serialize
is logging a model with data. If I expand model._data.attributes
in console I see the name
property with the value it was defined, but if I model.get('name')
, it’s undefined while model.get('id')
gives me the correct value.
My Router looks like this:
App.Router.map(function() {
this.route('about');
this.resource('posts', { path: 'blog' }, function() {
this.route('post', { path: ':year/:month/:post_id/:title' });
});
this.route('tag', {path: 'tags/:tag_name' });
});
The posts.post
route also has a specific serialization in PostRoute#serialize
, defined as follows:
serialize: function(model) {
var i = model.get('id');
var m = model.get('dateAdded').getMonth() + 1; if(12 < m) { m = 1; }
var y = model.get('dateAdded').getFullYear();
// don't crucify me. I'm not using Em.String.dasherize due to commas and dots, and this is an experiment
var t = model.get('title').replace(/,/gi, '').replace(/\./gi, '').replace(/ /gi, '-').toLowerCase();
return {
year: y, month: m, post_id: i, title: t
}
}
However it generates the correct url.
I’m not quite sure it has anything with the hasMany
since the property I’m trying to get is not in a relationship. I’m just curious because this issue doesn’t occur in the posts.post
route.
Has anyone see anything like this? How did you about it? Or am I doing something terribly wrong?
Environment:
DEBUG: -------------------------------
DEBUG: Ember.VERSION : 1.0.0-rc.3
DEBUG: Handlebars.VERSION : 1.0.0-rc.3
DEBUG: jQuery.VERSION : 1.9.1
DEBUG: -------------------------------