gpt4 book ai didi

Backbone.js - 从集合创建中获取 id

转载 作者:行者123 更新时间:2023-12-01 12:52:05 25 4
gpt4 key购买 nike

我正在使用 create 方法将模型添加到集合中,并且 api 响应正常。该模型似乎已正确返回并看到 console.dir( resp ); 这正是我正在寻找的。但是,当我尝试访问 runningorderid(用 idAttribute 定义的 id)时,响应为空。我认为这与响应的异步性质有关,但我不知道如何处理。

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

screenscape of problem

最佳答案

collection.create,因为所有与服务器请求相关的方法,确实是异步的。在您的情况下,您可以收听同步事件以获得所需的结果。

来自 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11628076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com