HaoQi Li
HaoQi Li

Reputation: 12350

Error with loading data in Ember local storage adapter

I'm trying to load some data, but I'm getting this error:

Uncaught Error: Attempted to handle event `loadedData` on <App.Person:ember295:1q697> while in state rootState.loaded.created.inFlight. Called with undefined 

The loading occurs like this:

App.then(function(){
  App.mystuff = ['Nina', 'Paul', 'Zoe'];

  App.mystuff.forEach(function(item){
    console.log("this is the item:");
    console.log(item)
    var p = App.Person.createRecord({name: item})
    p.save(); // just save on LS Adapter
  });
  console.log("Were they added?");
  console.log(App.Person.find());
});

You can see the app in this JSbin. Do you know how to fix it?

Basically I would like to know how to get App.Person.find() to work in the console and the code. I'm not getting results anywhere so far.

Possibly related.

Upvotes: 1

Views: 468

Answers (3)

Mike Grassotti
Mike Grassotti

Reputation: 19050

I'm trying to load some data, but I'm getting this error... Do you know how to fix it?

Seems the localstorage adapter has trouble a record is accessed while inFlight. That is surprising but pretty easy to work around. Since you are trying to load data into local storage when app is initialized, suggest loading it from the route's beforeModel hook:

beforeModel: function() {
  return App.mystuff.map(function(i) {
    return App.Person.createRecord({name: i}).save();
  });
},

Also, suggest specifying an id with each record, otherwise you will end up creating duplicate local copies. Something like this should work:

beforeModel: function() {
  return App.mystuff.map(function(i) {
    return App.Person.createRecord({id: i, name: i}).save();
  });
},

See this jsbin for a working example: http://jsbin.com/ofemib/2/edit

Basically I would like to know how to get App.Person.find() to work in the console and the code. I'm not getting results anywhere so far.

In general, you can get a model's find to method to work from console passing a function to it like this:

 App.Person.find().then(function(results) { console.log(results.get('length')) });

That's not really a local storage thing, just good practice for working with something that is async.

Upvotes: 1

Gevious
Gevious

Reputation: 3252

The reason its failing is that ember is trying to access the data in App.Person.find() while the data is being saved. Its a funny scenario, since an application usually doesn't create records itself, rather they're either created at startup (think fixtures) or they are created by the user within a page, not during a transition.

I suggest using ember's fixtureadapter for initial testing and once you have your application working nicely with the fixtures, to then move to your storage of choice. That way you can concentrate on the important aspect of creating your application and worry about persistance later.

Upvotes: 0

Kingpin2k
Kingpin2k

Reputation: 47367

I'm not sure if this helps, but I've thrown together a few examples. I updated a few of the random libraries, but the general idea is still there.

JSBin

Upvotes: 0

Related Questions