1
votes

I am experiencing a weird issue while using ember data. With the following user model everything works great.

App.User= DS.Model.extend({
    firstName: attr(),
    lastName: attr()
});

I call user.save() and is posts to /users with the correct data. However when i try and use a user model that has relationships on it

App.User= DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    friends: DS.hasMany('user'),
    followers: DS.hasMany('user'),
});

For some reason with that model when i call user.save() it posts to /Users (note the capitalization. Also, in the response it expects it formatted {"User": {...}} instead of {"user": {...}}

Anyone run into this before? I could always add the additional endpoints to my api however I would like it to work uniform if possible.

1
I'm not seeing this Rob, can you update my jsbin to replicate? emberjs.jsbin.com/OxIDiVU/151/edit - Kingpin2k
i found the issue kingpin2k. please see my explanation below. thanks for setting up the jsbin though. - Rob

1 Answers

0
votes

I did a little more digging and it seems when you add a relationship to the model there is a computed property called relationshipsByName. This property, in my example, will set the meta.type property to 'User'. It works without relationships because I called the createRecord method with 'user' so i assume it uses this as the type. When the relationship is added it uses 'User'

I found that modelFor calls the resolvers normalize on the keys. So the solution is to add a custom resolver like below.

App = Ember.Application.create({
     Resolver: Ember.DefaultResolver.extend({
         normalize: function(fullName) {
           var n = this._super(fullName);
           if(fullName.startsWith('model')){
               n = n.replaceAt(6, n[6].toLowerCase());
            }
            return n;
           }
      })
}); 

*note i have string extensions for startsWith and replaceAt