Backbone 中的计算属性 [英] Computed properties in Backbone

查看:21
本文介绍了Backbone 中的计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,在客户端上处理的数据以不同于在服务器上表示的方式呈现和交互.

I have a scenario where the data being manipulated on the client is presented and interacted with in a different way than it is represented on the server.

考虑从服务器返回的以下 event 资源.

Consider the following event resource returned from the server.

{
  "id": 123,
  "start_at": 1331336004906,
  "end_at": 1331337704906
}

以及以下用于编辑的模板:

And the following template for editing:

<form>
  <!-- Notice how date and time are separated in the interface -->
  <input type="text" name="start_date" value="{{start_date}}" />
  <input type="text" name="start_time" value="{{start_time}}" />

  <!-- Instead of asking for an end date/time, we ask for the duration -->
  <input type="text" name="duration" value="{{duration}}" />

  <input type="submit" />
</form>

如何将 start_datestart_timeduration 作为我的 Backbone 模型中的属性而不将它们发送到服务器?我应该修改 .toJSON() 吗?

How would I go about treating start_date, start_time, and duration as attributes in my Backbone model without sending them to the server? Am I supposed to modify .toJSON()?

推荐答案

你的模型应该尽可能地与服务器端的模型一致.所以坚持使用 start_atend_at.这将大大简化您的 sync() 操作.

Your model should correspond as closely as possible to what you have server side. So stick with start_at and end_at. That will greatly simplify your sync() operations.

在编辑表单的查看中,您可以:

On your edit form's View, you can:

  1. 通过简单的函数计算start_datestart_timeduration并在模板中调用它们.
  2. 提交时转换为 start_atend_at.
  1. Compute start_date, start_time, duration through simple functions and call them in the template.
  2. Convert to start_at and end_at on submitting.

这篇关于Backbone 中的计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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