Skip to content

Ember-cli, retrieving a model from the store does not set adapter and url properties #415

@gabriel-dehan

Description

@gabriel-dehan

Hello there,

I am building an ember-cli project (well actually an ember-addon) using ember-model and have been having some troubles making it work with the ES6 module system provided by ember-cli.

TL;DR : Retrieving a model through the store does not correctly set the adapter and url attributes. Reopening the model class and setting the attributes solves it. But if you actually import the model in your router (for instance) and retrieve the model by this way, the adapter and url are actually set. Why ?

What precisely happens is :

// Route

import Ember from "ember";                                                                                                                                                                   

export default Ember.Route.extend({                                                                                                                                                          
  model: function() {                                                                                                                                                                                                                                                                                                              
    return this.get('store').modelFor('myModel').find(2);                                                                                                                                             
  },                                                                                                                                                                                         
  setupController: function(controller, model) {                                                                                                                                             
    controller.set('model', model);                                                                                                                                                         
  }                                                                                                                                                               
});                                                                                                                                                                                          
// Model

import Ember from "ember";                                                                                                                                                                   

var Model = Ember.Model.extend();                                                                                                                                                                                          

Model.url = '/model/url';                                                                                                                                                                
Model.adapter = Ember.RESTAdapter.create();                                                                                                                                            

export default Model;    

I get an Ember.Adapter must implement find Error: Ember.Adapter must implement find.

I have checked and it seems that this.store.modelFor('replay') correctly returns the model but without the attributes url and adapter set. Thus we get the default adapter (which only implements the interface) and an undefined URL.

It can be fixed by doing the following :

// Model

import Ember from "ember";                                                                                                                                                                   

var Model = Ember.Model.extend();    

Model.reopenClass({                                                                                                                                                                  
  url: '/model/url',                                                                                                                                                                           
  adapter: Ember.RESTAdapter.create()                                                                                                                                                        
});

export default Model;    

But I don't understand what causes it.

Furthermore, if I don't use the store to retrieve the model ( this.store.modelFor('replay') ) but instead import the model file :

// Route
import Ember from "ember";
import MyModel from "../../models/my-model";

export default Ember.Route.extend({                                                                                                                                                          
  model: function() {                                                                                                                                                                        
      return MyModel.find(2); // Or something like that
  }
});

It properly works and has the attributes set without needing to reopen the class.
BUT the container is actually not set for the models that are not retrieved through the store, and it then fails when fetching relations and other stuffs.

I have been having a lot of trouble with all this. I am totaly new to Ember and if someone has an explanation on why, how, I'd be delighted and could maybe provide a fix as seen fit : )

Have a good day !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions