主要成分:如何更新,如果没有获取整个集合是否新款模型? [英] Backbone: How to update a model that isNew without fetching entire collection?

查看:68
本文介绍了主要成分:如何更新,如果没有获取整个集合是否新款模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,创建为集合的典范。这触发一个添加事件集合。我有一个绑定到一个方法:

I have a form that create a model for a collection. That fires an add event to the collection. I have that binded to a method:

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

创建它同步到数据库中,但是新的附加观点仍然是否新款来的骨干,因为它不是招 ŧ获取的收集,抓住id为新的模式。

The create syncs it to the database but, the new appended view still isNew to backbone since it hasn't fetched the collection and grabbed the id for the new model.

我的问题,我怎么能抓住从服务器同步模式(即有编号,而不是是否新款)并不获取整个集合以及追加呢?

My question, how can I grab the synced model from the server (that has the id and isn't isNew) without fetching the entire collection then append it?

推荐答案

使用同步事件,而不是添加 ...

Use the sync event instead of add...

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

添加事件被炒鱿鱼的立即的调用时创建;但 同步一旦服务器已经响应create方法被炒鱿鱼,所以它应该包括正确的型号标识

The add event gets fired immediately when calling create; but sync gets fired once the server has responded to the create method, so it should include the correct model id.

您也可以等待服务器的响应,加入模型前集合 - 要做到这一点使用等待:在真正创建的选项哈希: collection.create({...},{等待:真})

You can also wait for the server's response, before adding the model to the collection -- to do that use wait: true in create's options hash: 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.

这篇关于主要成分:如何更新,如果没有获取整个集合是否新款模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆