I am trying to use the slug as the primary key for my app and having no joy.
Using the code below I am getting the error → Cannot read property ‘slug’ of null, and it seems to be trying to query my api via the notices id, not slug. This makes no sense to me as I have gone as far as to completely remove id from sending and I am still getting it querying my api using the id not slug… ( yes I have cleared my cache )
If anyone has any ideas as to why this is doing this that would be great, it seems to work fine if I work by ID not slug but only for already loaded content.
Code:
//Init Ember Application
var App = Ember.Application.create();
App.nConfig = noticesConfig;
App.Router.reopen({
rootURL: App.nConfig.rootUrl+'/'
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: App.nConfig.restUrl,
headers: {
'Accept': 'application/json'
}
});
//Ember Routes
App.Router.map(function() {
this.resource('index', {path: '/'}, function () {
this.resource('single', { path: '/single/:notice_id' });
});
});
App.Medium = DS.Model.extend({
filename : DS.attr('string'),
folder : DS.attr('string'),
extension : DS.attr('string'),
filePath : function(){
return App.nConfig.fileUrl+'/'+this.get('folder')+'/'+this.get('filename');
}.property('filename','folder'),
notice : DS.belongsTo('notice', {async: true})
});
App.Readership = DS.Model.extend({
email : DS.attr('string'),
username : DS.attr('string'),
viewed : DS.attr(),
confirmed : DS.attr(),
notice : DS.belongsTo('notice', {async: true})
});
App.Notice = DS.Model.extend({
name : DS.attr('string'),
author : DS.attr('string'),
shortDesc : DS.attr('string'),
bodyContent : DS.attr('string'),
created : DS.attr('string'),
unread : DS.attr(),
media : DS.hasMany('medium', {async: true}),
readerships : DS.hasMany('readership', {async: true})
});
App.NoticeSerializer = DS.RESTSerializer.extend({
primaryKey: 'slug',
normalize: function(type, hash) {
hash.id = hash.slug;
return this._super(type, hash);
}
});
App.IndexRoute = Ember.Route.extend({
model: function() {
var notices = this.store.find( 'notice', { type: App.nConfig.type, limit: 20 });
return notices;
}
});
App.SingleRoute = Ember.Route.extend({
model: function( params ) {
var notice = this.store.find( 'notice', params.notice_id );
return notice;
}
});