问题描述
我有一个场景,在客户端上处理的数据以不同于在服务器上表示的方式呈现和交互.
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_date
、start_time
和 duration
作为我的 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_at
和 end_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:
- 通过简单的函数计算
start_date
、start_time
、duration
并在模板中调用它们. - 提交时转换为
start_at
和end_at
.
- Compute
start_date
,start_time
,duration
through simple functions and call them in the template. - Convert to
start_at
andend_at
on submitting.
这篇关于Backbone 中的计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!