在以ember保存模型时尝试调用loadedData [英] Attempt to call loadedData when saving models in ember

查看:89
本文介绍了在以ember保存模型时尝试调用loadedData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程式中收到这个错误,但不知道是什么原因造成的。

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

每当我提交我的数据存储时都会发生:

It happens whenever I commit my data store:

尝试处理事件 loadedData ,而状态为rootState.loaded.updated.inFlight。调用未定义

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

任何人?

这是导致它的代码:

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();
}

修改

我已经把它煮到了导致错误的以下代码(用coffeescript写))。事情是,我第一次运行代码时,当对象首先不存在于数据存储中时,它运行正常。当我在已经包含具有指定ID的记录的数据存储上运行代码时,会发生错误。这可能会帮助您解读发生了什么。

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()

编辑2

创建记录的类似问题:

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

编辑3

所以事实证明,在提交我认为是导致问题的商店之后,我正在调用 @timestamp = ts_setting.get('value')因为我试图从尚未保存的对象中获取一些数据。

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.

推荐答案

加载数据是异步的。 Cluey.Setting.find 返回一份XHR请求成功后将被解决/更新的承诺。

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

所以调用 Cluey.Setting.find ,修改它( ts_setting.set(value,0))然后在某个时间点,您将从服务器获取结果(来自 find 的响应)。

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).

那时候,ember数据引起了一个错误,因为它无法更新被修改的记录。

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();
});

这篇关于在以ember保存模型时尝试调用loadedData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆