获取Ember-Data REST响应值 [英] Get Ember-Data REST response value
问题描述
Ember:1.0.0-rc.6
Ember-Data:e999edb(2013-07-06 06:03:59 -0700)
我进行一个REST调用(POST)登录用户。
服务器响应确定。
我需要从服务器的ID,但我只得到ID为setTimeout。
我认为这不是正确的方式。 >
我的错误是什么?
在控制器内调用:
var login = App.Login.createRecord(this.getProperties(email,password));
login.on(didCreate,function(record){
console.log(record.get(id)); // ID为null
控制台。 log(record.get(email));
});
setTimeout(function(){
console.log(login.get(id)); // ID可用
console.log(login.get(电子邮件));
},500);
DS.defaultStore.commit();
你是对的 - 有一个bug数据,其中 materializeData
事件,其中基本设置id和解包服务器响应不会发生,直到 didCreate
回调。所以发生的情况是,在您的 login.on(didCreate....)
回调中,记录尚未实现。
这似乎仍然是一个问题 - 有关详细信息,请参阅此主题: https://github.com/emberjs/data/issues/405#issuecomment-17107045
解决方法
您的工作正常,但更简单(更干净?)是将您的回调操作包含在$ code > Ember.run.next :
login.on(didCreate,function(record) {
Ember.run.next(function(){
console.log(record.get(id)); //应该设置ID
console.log(record.get (email));
}
});
,至少你不需要处理超时。
我相信这可以通过延迟这个动作直到下一个运行循环,然后实现应该有已经发生了。更多关于Ember运行循环
资料来源: https://github.com/emberjs/data/issues/405#issuecomment-18726035
Ember: 1.0.0-rc.6
Ember-Data: e999edb (2013-07-06 06:03:59 -0700)
I make a REST call (POST) to login a user. Server response is ok. I need the ID from the server, but i only got the ID with "setTimeout".
I think this is not the right way.
What is my mistake?
Within Controller i call:
var login = App.Login.createRecord(this.getProperties("email", "password"));
login.on("didCreate", function(record) {
console.log(record.get("id")); // ID is null
console.log(record.get("email"));
});
setTimeout(function() {
console.log(login.get("id")); // ID is available
console.log(login.get("email"));
}, 500);
DS.defaultStore.commit();
You're right -- there's a bug in ember-data where the materializeData
event, which basically sets the id and unwraps the server response doesn't happen until AFTER the didCreate
callback. So what's happening is that in your login.on("didCreate" ....)
callback, the record still hasn't materialized yet.
This seems to still be an issue -- see this thread for more information: https://github.com/emberjs/data/issues/405#issuecomment-17107045
Workaround
Your work around is fine, but an easier (cleaner?) one is to wrap your callback actions in a Ember.run.next
:
login.on("didCreate", function(record) {
Ember.run.next(function() {
console.log(record.get("id")); // ID should be set
console.log(record.get("email"));
}
});
This way, at least you don't need to deal with the timeout.
I believe this works by delaying the actions until the next run loop, and by then the materialization should have already happened. More on the Ember run loop
Source: https://github.com/emberjs/data/issues/405#issuecomment-18726035
这篇关于获取Ember-Data REST响应值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!