2
votes

the page load starts at when a user is viewing a user's profile. And he makes some action and my code does ajax call to update it's user type-

App.UserController = Ember.ObjectController.extend
  convert: ->
    $.get '/api/user/convert_to_client/' + id, (response) ->
      self.set('user_type', response.user.user_type)     

But whenever i go to the user listing table and Ember tries to fetch all the users from the UsersRoute:

module.exports = App.UsersRoute = Em.Route.extend
    model: ->
      App.User.find({}).then (response) ->     
        self.controllerFor('users').set('content', response)       

Then i end up getting all errors similar to these:

Error Attempted to handle event `loadedData` : Object not updated after deleteRecord

Update ember-data model\

I think this article here explains the issue -

http://www.thomasboyt.com/2013/05/01/why-ember-data-breaks.html

Uncaught Error: Attempted to handle event loadedData on while in state rootState.loaded.updated.uncommitted. Called with {}

What this means is that you're trying to do something to the record that it can't do in its current state. This often happens when trying to update a record that's currently saving or when trying to render a property on an object that's been deleted.

But note that when its the other way around, I first go to the user listing table, and then go and view a user's profile, update the user - this error never comes out.

Edit:

Sample response from users:

{
  users: [
    {
       _id: 521e1112e8c5e10fb40002a0
        ..
    }
   ]
}

and for a single user:

{
  user: {
    _id: 521e1116e8c5e10fb40004ca
  }
}
2
Could you post the exact error you are getting? - chopper
this is the error - Uncaught Error: Attempted to handle event loadedData on <User:ember323:521e110de8c5e10fb4000029> while in state rootState.loaded.updated.uncommitted. Called with undefined - David
Could you also include you model definition as well as the JSON responses for the two requests? - chopper
Ive edited my post on a few places - first i added the sample json result, which conforms to the format of ember data. 2nd - scroll all the way up, you will see m ajax call is made inside the UserController. - David

2 Answers

0
votes

You should set up your controller in the `setupController' hook like this:

model: function() {
   return App.User.find({});
},
setupController: function(controller, model) {
   controller.set('content', model);
}

Not sure if that's the only problem though. Could you post more information on the error you are getting?

0
votes

You should use store.update to update the record in the store without changing the record's state (instead of using set on the record) e.g.

$.get '/api/user/convert_to_client/' + id, (response) =>
  @store.update 'user', id: id, user_type: response.user.user_type

Or, if you structure your response correctly, just:

@store.update 'user', response.user

Note: update may not be available in older versions of EmberData