Sean Mackesey
Sean Mackesey

Reputation: 10939

Ember Data RESTAdapter is not Submitting Requests to the Server when I run Model#find()

I am developing an Ember app. I am serving the API and the ember app from the same server on localhost.

I am pretty sure my API conforms to Ember specifications (although I'm not sure, because conflicting information is given here and here. The second site is linked to from this Ember blog post, and is supposed to be a formalization of Ember's conventions). My API follows the style of the second link, so requesting host/api/users/1, for instance, returns:

{
  "users": [{
      "username": "jojo"
      etc..
  }]
}

I know my API is working because I can test it with curl. My problem is that Ember data doesn't seem to be making requests. The Chrome developer tools network tab shows that no request to load the data is being sent when I run App.User.find(). I do however get back this:enter image description here

Here is the coffeescript code where I define the store:

App.Store = DS.Store.extend
  revision: 13
  url: 'http://localhost:9292/'
  adapter: DS.RESTAdapter

DS.RESTAdapter.reopen
  namespace: 'api'

And here is where I define the model, controller, and route for 'User':

App.UsersRoute = Ember.Route.extend
  model: ->
    App.User.find()

App.UsersController = Ember.ArrayController.extend()

App.User = DS.Model.extend
  username: DS.attr 'string'
  url: DS.attr 'string'
  projects: DS.hasMany 'App.Project'
  query_instances: DS.hasMany 'App.QueryInstance'

Am I missing something important here? I am new to Ember and client-side development in general-- perhaps there are obvious solutions here but I'm not aware of them. I'd appreciate if anyone could give a few tips on debugging Ember in general.

Upvotes: 0

Views: 1154

Answers (2)

Sean Mackesey
Sean Mackesey

Reputation: 10939

I was able to solve this problem by upgrading to more recent versions of Ember and Ember data. I think that I was using incompatible versions. I initially generated my project using the Yeoman ember generator. I seriously doubt that this generator would actually give you incompatible versions, so I'm not quite sure what the problem was. It may have been:

  • An issue with my bower cache
  • An incorrect statement of the revision number in the DS.Store declaration, which I may have changed because I was unclear as to whether this needed to be included. It is not needed in the latest version, but that's not what the Yeoman generator was providing. So I may have deleted it, then added it back with the wrong version number.

In any case, for anyone else having similar problems I advise you to upgrade your versions of Ember and Ember Data and ensure that they are compatible.

Upvotes: 1

intuitivepixel
intuitivepixel

Reputation: 23322

Since the framework is very jung there are still not so much tools besides the browser console and a debugging affiniti to debug ember applications, but some stuff exists that can help out with this task.

1. Not nearly ready for prime time but very usefull to inspect ember.js app's is the ember extension for chrome.

2. Since this PR it exists the possibility to set the flag LOG_ACTIVE_GENERATION to get information about what get's generated by ember under the hood. The active generation logging can be simply enabled with:

App = Ember.Application.create({
  LOG_ACTIVE_GENERATION: true
});

3. For debugging only but very usefull is the __container__, one can inspect everything ember-data knows about it's models with ease like this:

App.__container__.lookup('store:main').recordCache

This will give you access to the store's recordCache, e.g. all the loaded records. To inspect more in detail you can access specific records like this:

App.__container__.lookup('store:main').recordCache[7].get('data.attributes')

4. One more thing might be also usefull is to define/override the global error handler to catch all errors happening inside an ember app:

Ember.onerror = function(error) {
  console.log(error);
}

5. There is also the possibility to log records directly in your templates by using the log handlebars helper

{{log post}}

or simply set a debugger breakpoint to inspect further

{{debugger}}

Hope this information helps you debug successfully your app.

Upvotes: 1

Related Questions