Backbone.Model.save,不更新客户端 [英] Backbone.Model.save and don't update client

查看:145
本文介绍了Backbone.Model.save,不更新客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话保存中的骨干模型,并把它写数据到服务器,但的的更新客户端。我该怎么做呢?

I want to call save on a Backbone model and have it write data to the server, but not update the client. How do I do this?

要阐明:当主干保存一个模型,它的所有数据发送到服务器,然后从服务器和客户端的更新它检索数据。这第二个步骤是我想不会发生的。

To clarify: when Backbone saves a model, it sends all the data to the server, and then retrieves the data from the server and updates it on the client. This second step is what I want not to happen.

要进一步明确:模型(父模型)有它本身就是一个模型('孩子'模型)的属性;当它保存到服务器上,这孩子模型转换成JSON。当后父模型更新保存,即previously包含的子模型的引用将被替换为所保存的子模型的解析的JSON对象的属性。这是我不需要的情况发​​生。

To clarify further: The model ('parent' model) has an attribute which is itself a model ('child' model); when it's saved to the server, this child model is converted to JSON. When the parent model updates after the save, the attribute that previously contained a reference to the child model is replaced with the parsed JSON object of the child model that was saved. This is what I need not to happen.

当数据是<青霉>最初的从服务器拉时,父模式重新构造该对象到适当的子模型,但这是一个昂贵的过程,并且没有理由重新做每次保存父模型火灾,因为子模型永远不会改变。

When the data is initially pulled from the server, the parent model "reconstitutes" that object into an appropriate child model, but this is an expensive process and there is no reason to re-do it every time save fires on the parent model, since the child model will never change.

推荐答案

这听起来像你当你收到来自服务器的响应不想解析模型一个 model.save

It sounds like you do not want to parse your model when you receive the response from the server on a model.save

您可以尝试的东西,如:

You can try something such as:

model.save(attributes,{
  success: function() { ... },
  parse  : false     // This will be passed to your parse function as an option
});

您必须设置您的解析函数在相应的模式如下:

You would have to set-up your parse function in your corresponding model as follows:

parse: function(resp,options) {
  // don't update model with the server response if {parse:false} is passed to save
  if (options && !options.parse) return;

  / ... rest of your parse code ... /

目前骨干默认 options.parse 真正这里是关于这一主题的短讨论。

Backbone currently defaults options.parse to true. Here is a short-discussion on the topic.

由于在该线程的讨论,也许你要考虑的为什么的你不希望要更新的服务器响应到客户端。有可能实现你的愿望的结果的更清洁的方式。

As discussed in that thread, perhaps you want to consider why you do not want want to update the server response to the client. There may be a cleaner way to achieve the results you desire.

这篇关于Backbone.Model.save,不更新客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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