对于骨干嵌套属性模板 [英] Templates for nested attributes with Backbone

查看:185
本文介绍了对于骨干嵌套属性模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预约模式,每个实例,其中有一个客户端。这里是我的模板编辑预约:

I have an Appointment model, each instance of which has a Client. Here's my template for editing an appointment:

<form id="edit-appointment" name="appointment" class="mobile_friendly">
  <div class="field">
    <input type="text" name="start_time_ymd" id="start_time_ymd" value="<%= start_time_ymd %>" placeholder="Start Date">
    <input type="text" name="start_time_time" id="start_time_time" value="<%= start_time_time %>" placeholder="Start Time">
  </div>

  <div class="field">
    <input type="text" name="client_name" id="client_name" value="<%= client.name %>" placeholder="Client">
    <input type="hidden" name="client_id" id="client_id" value="<%= client.id %>" placeholder="Client ID">
  </div>

  <div class="field">
    <input type="text" name="client_phone" id="client_phone" value="<%= client.phone %>" placeholder="Phone">
  </div>

  <div class="field">
    <input type="text" name="notes" id="notes" value="<%= notes %>" placeholder="Notes">
  </div>

  <div class="actions">
    <input type="submit" value="Update Appointment" />
  </div>

</form>

<a href="#/index/<%= stylist_id %>">Back</a>

我的问题是我的客户端属性不会被传递到服务器正确。获取传递到保存服务器的JSON对象看起来像下面这样。让我们pretend我有一个客户端的电话号码 555-555-5555 ,但我将其更改为 555-555-1234 ,然后提交表单:

My problem is that my Client attributes aren't being passed to the server correctly. The JSON object that gets passed to the server on save looks something like the following. Let's pretend I have a client with phone number 555-555-5555 but I change it to 555-555-1234 and then submit the form:

{"appointment":
  {"start_time_ymd":"1/1/2000",
   "client_phone":"555-555-1234",
   "client":{"phone":"555-555-5555"}}

我省略了很多不相关的领域,但我希望你明白我的意思。我已经改变了 client_phone 555-555-5555 555-555-1234 ,而客户端对象的JSON对象有其电话号码不变。我需要以某种方式改变电话号码。

I've omitted a lot of irrelevant fields, but hopefully you see what I mean. I've changed client_phone from 555-555-5555 to 555-555-1234, but the client object in the JSON object has its phone number unchanged. I need to somehow change that phone number.

我如何使这些领域 - 如电话号码字段 - 实际上是取,使他们获得在<$ C传递到服务器作为客户端对象的一部分$ C>预约,而不是直接下预约?我使用的骨干关系,如果有差别。

How do I make these fields - like the phone number field - actually "take" so they get passed to the server as part of the client object under appointment and not directly under appointment? I'm using Backbone-relational, if that makes a difference.

推荐答案

我能得到像这样的工作:

I was able to get it to work like this:

class Snip.Views.Appointments.EditView extends Backbone.View
  template : JST["backbone/templates/appointments/edit"]

  events :
    "submit #edit-appointment" : "update"

  update : (e) ->
    e.preventDefault()
    e.stopPropagation()

    # THE ONLY CHANGE I MADE WAS TO ADD THIS LINE
    @model.attributes.client.phone = $("#client_phone").val()

    @model.save(null,
      success : (appointment) =>
        @model = appointment
        window.location.hash = "/#index/" + @model.attributes.stylist_id
    )   

这篇关于对于骨干嵌套属性模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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