将更新后的模型添加到 Backbone 个集合中
add updated model into Backbone Collections
我使用collection create
方法将模型保存到服务器中。但是这个 create
方法会自动将 stale model
添加到 collection
中。
我正在尝试做以下事情。
插入过程成功后,我想将updated
model
添加到集合中。更新的信息从服务器返回
作为 json
.
如果服务器响应 error
,我不想将 model
添加到
collection
.
我需要扩展 collection create
方法吗?或者有别的办法吗?
来自文档:
Creating a model will cause an immediate "add" event to be triggered on the collection, a "request" event as the new model is sent to the server, as well as a "sync" event, once the server has responded with the successful creation of the model. Pass {wait: true} if you'd like to wait for the server before adding the new model to the collection.
(强调我的)
看起来像:
collection.create({ // attributes
},{wait: true});
我使用collection create
方法将模型保存到服务器中。但是这个 create
方法会自动将 stale model
添加到 collection
中。
我正在尝试做以下事情。
插入过程成功后,我想将
updated model
添加到集合中。更新的信息从服务器返回 作为json
.如果服务器响应
error
,我不想将model
添加到
collection
.
我需要扩展 collection create
方法吗?或者有别的办法吗?
来自文档:
Creating a model will cause an immediate "add" event to be triggered on the collection, a "request" event as the new model is sent to the server, as well as a "sync" event, once the server has responded with the successful creation of the model. Pass {wait: true} if you'd like to wait for the server before adding the new model to the collection.
(强调我的)
看起来像:
collection.create({ // attributes
},{wait: true});