Skip to content

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

Open

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 !

Activity

  1. changed the title [-]Ember-cli, retrieving a record from the store does not set adapter and url properties[/-] [+]Ember-cli, retrieving a model from the store does not set adapter and url properties[/+] on Dec 11, 2014
  2. williscool commented on May 9, 2015

    @williscool

    I think this is happening to me as well.

    In my case its a model that was copying into an app from an addon.

    But ember just complains about Fixtures not being set for the model

  3. danii1 commented on May 14, 2015

    @danii1

    There are many quirks with Ember Model in Ember Cli, furthermore:
    Model.find(paramsHash) doesn't cache records in the store, while this.store.find('model', paramsHash) works as expected and records are being pushed to the store.

  4. spectras commented on Oct 2, 2015

    @spectras
    Contributor

    Doing this solves the issues with adapter for me:

    Ember.Application.instanceInitializer({
        name: 'data-adapter-load-models',
        initialize: function (app) {
            var store = app.container.lookup('store:main'),
                knownModels = app.registry.knownForType('model');
            Object.keys(knownModels).forEach(function (model) {
                model = model.split(':', 2)[1];
                store.modelFor(model).reopenClass({
                    adapter: store.adapterFor(model)
                });
            });
        }
    });

    But that's no permanent solution. A better way would be to inject the adapter in ember-model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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