gpt4 book ai didi

javascript - Backbone : How to update a model that isNew without fetching entire collection?

转载 作者:行者123 更新时间:2023-11-30 10:36:45 24 4
gpt4 key购买 nike

我有一个表单可以创建 一个集合模型。这会向集合触发一个 add 事件。我将其绑定(bind)到一个方法:

this.collection.bind('add', this.addOne, this)

开火...

addOne: function(tenant) {
var self = this
var collection = this.collection

var view = new TenantView({model: tenant,collection:collection});
self.$el.append(view.render().el);
}

create 将它同步到数据库,但是,新的附加 View 仍然是 isNew 到主干,因为它没有获取集合并获取新模型的 id .

我的问题是,如何从服务器(具有 ID 且不是 isNew)获取同步模型而不获取整个集合然后附加它?

最佳答案

使用 sync 事件代替 add...

this.collection.bind('sync', this.addOne, this);

add 事件在调用 create 时被立即触发;但是sync gets fired once the server has responded to the create method, so it should include the correct model id .

您还可以等待服务器的响应,然后再将模型添加到集合中——为此,请在 create 的选项哈希中使用 wait: true:collection.create({ ... }, { wait: true }).

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. Pass {wait: true} if you'd like to wait for the server before adding the new model to the collection.

关于javascript - Backbone : How to update a model that isNew without fetching entire collection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599744/

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