Backbone.js的 - 从集合获取ID创建 [英] Backbone.js - getting id from collection create
问题描述
我使用的是创建
方法将一个模型集合,API为响应就好了。该模型似乎已经正常返回,请参阅 console.dir(RESP);
这是我一直在寻找。然而,当我尝试访问 runningorderid
,这是 ID
与定义的 idAttribute
,响应为空。我presume这事做响应的异步性,但我不知道该如何处理它。
I am adding a model to a collection using the create
method and the api is responding just fine. The model seems to have been properly returned and see the console.dir( resp );
which is what I was looking for. However, when I try to access runningorderid
, which is the id
as defined with idAttribute
, the response is null. I presume this is something to do with the async nature of the response, but I don't know how to deal with it.
var resp = window.app.RunningOrderCollection.create(
{ runningorderid: null, listitemid: 1, starttime: n} ,
{ wait: true }
);
console.dir( resp );
console.dir( resp.get("strt") );
console.dir( resp.id );
推荐答案
collection.create
,由于涉及到服务器的请求的所有方法,的确是异步的。你的情况,你可以听同步事件得到期望的结果。
collection.create
, as all methods related to server requests, is indeed asynchronous. In your case, you could listen to the sync event to get the desired result.
http://backbonejs.org/#Collection-create
创建模型将导致触发的直接添加事件
上的收集,以及一个同步事件中,一旦模式已经
在服务器上创建成功。
Creating a model will cause an immediate "add" event to be triggered on the collection, as well as a "sync" event, once the model has been successfully created on the server.
例如:
resp.on('sync', function(model) {
console.dir( resp );
console.dir( resp.get("strt") );
console.dir( resp.id );
});
这篇关于Backbone.js的 - 从集合获取ID创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!