l33z3r
l33z3r

Reputation: 615

Attempt to call loadedData when saving models in ember

I keep getting this error in my app but have no idea what is causing it.

It happens whenever I commit my data store:

Attempted to handle event loadedData on while in state rootState.loaded.updated.inFlight. Called with undefined

Anyone?

Here is the code that causes it:

var ts_setting;

ts_setting = Cluey.Setting.find(Cluey.SettingsKeyIDs.API_TIMESTAMP);

if (ts_setting.get('value') != null) {
  console.log("Found Timestamp");
} else {
  console.log("Creating Initial Timestamp...");
  ts_setting.set("id", Cluey.SettingsKeyIDs.API_TIMESTAMP);
  ts_setting.set("value", 0);
  Cluey.store.commit();
}

Edit

I have boiled it down to the following code (written in coffeescript) that is causing the error. The thing is, that the first time I run the code, when the object first does not exist in the data store, it runs fine. The error then happens when I run the code on a data store that already contains a record with the specified id. This might help you decipher what is happening.

ts_setting = Cluey.Setting.find(Cluey.SettingsKeyIDs.API_TIMESTAMP)
ts_setting.get('value')
ts_setting.set("id", Cluey.SettingsKeyIDs.API_TIMESTAMP)
ts_setting.set("value", 0)
Cluey.store.commit()

Edit 2

I am having a similar problem creating a record:

ts_setting = Cluey.Setting.createRecord
    id: Cluey.SettingsKeyIDs.API_TIMESTAMP,
    value: 0

Cluey.store.commit()

The above code gives me this error:

Uncaught Error: Attempted to handle event `loadedData` on <Cluey.Setting:ember327:1> while in state rootState.loaded.created.inFlight. Called with undefined

Edit 3

So it turns out I was calling @timestamp = ts_setting.get('value') just after committing the store which I suppose was causing the issue, as I was trying to fetch some data from an object that had not yet been saved.

Upvotes: 2

Views: 1921

Answers (1)

Cyril Fluck
Cyril Fluck

Reputation: 1581

Loading data is asynchronous. Cluey.Setting.find returns you a promise that will be resolved/updated once the XHR request succeeds.

So you get your promise when calling Cluey.Setting.find, modify it (ts_setting.set("value", 0)) then at some point in time, you get the result from your server (the response from find).

At that moment, ember data raises an error because it can't update a record that is being modified.

What you want to do is to wait for your record to get completely loaded before modifying and saving it.

ts_setting = Cluey.Setting.find(Cluey.SettingsKeyIDs.API_TIMESTAMP);
ts_setting.one('didLoad', function() {
  ts_setting.set("value", 0);
  Cluey.store.commit();
});

Upvotes: 6

Related Questions