1
votes

Cross-posting from discuss.ember. I am using Ember 2.0.1 with Ember-data 2.0 and default the default RESTSerializer generated by ember-cli. I know this question has been asked to many places before (which none have real answers) but no solutions have been working for me yet.

I have this model hook for a user model :

export default Ember.Route.extend({
    model() {
    return this.store.findAll('user');
    }
});

Router is the following :

Router.map(function() {
  this.route('users', { path: '/' }, function() {
    this.route('user', { path: '/:user_id' }, function(){
    this.route('conversations', { path: '/'}, function(){
        this.route('conversation', { path: '/:conversation_id' });
    });
    });
  });
}); 

For example, going to /conversations/4 transitions to users.user.conversations. My relations are defined in my models. In the user model I have a DS.hasMany('conversation') conversations attribute set with { embedded: 'always' }. Returned JSON looks like this :

{"conversations":[
    {
     "id":183,
     "status":"opened",
     "readStatus":"read",
     "timeAgoElement":"2015-08-20T16:58:20.000-04:00",
     "createdAt":"June 16th, 2015 20:00",
     "user":
            {
               "id":4
            }
    }
   ]}

The problem I get is that Ember-data is able to add my data to the store but I get this error :

Passing classes to store methods has been removed. Please pass a dasherized string instead of undefined

I have read these posts : #272 and #261

Is it a problem with the JSON response?

Thank you. I have been using ember-data for quite a bit of time and never encountered this error before switching to ember 2.0.1 and ember-data 2.0.0

EDIT : I am now sure it is related to the embedded conversations because in ember inspector, if I try to see the conversations of a user (and the conversations are loaded into the store), it returns me a promiseArray which isn't resolved.

2

2 Answers

0
votes

Try not to push objects to store directly. Possible use-case of .push() :

For example, imagine we want to preload some data into the store when the application boots for the first time.

Otherwise createRecord and accessing attributes of parent model will load objects to the store automatically.

In your case UserController from backend should return JSON:

{"users" : [ {"id":1,"conversations":[183,184]} ]}

Ember route for conversation may look like:

export default Ember.Route.extend({

    model: function(params) {
        return this.store.find('conversation', params.conversation_id);
    }
}

User model:

export default DS.Model.extend({
    conversations: DS.hasMany('conversation', {async: true})
});

You don't have to always completely reload model or add child record to store. For example you can add new conversation to user model:

this.store.createRecord('conversation', {user: model})
                .save()
                .then(function(conversation) {
                    model.get('conversations').addObject(conversation);
                });

P.S. Try to follow Ember conventions instead of fighting against framework. It will save you a lot of efforts and nervous.

0
votes

Your conversation route has URL /:user_id/:conversation_id. If you want it to be /:user_id/conversations/:conversation_id, you should change this.route('conversations', { path: '/'}, function(){ to this.route('conversations', function(){ or this.route('conversations', { path: '/conversations'}, function(){