1

Appointmentモデルがあり、その各インスタンスにはClient. 予定を編集するための私のテンプレートは次のとおりです。

<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>

私の問題は、Client属性がサーバーに正しく渡されていないことです。保存時にサーバーに渡される JSON オブジェクトは次のようになります。電話番号を持つクライアントがいるふりをしましょうが、555-555-5555それを変更し555-555-1234てフォームを送信します。

{"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たがclient、JSON オブジェクト内のオブジェクトの電話番号は変更されていません。どうにかしてその電話番号を変更する必要があります。

電話番号フィールドのように、これらのフィールドを実際に「取る」ようにするにはどうすればよいclientですappointmentappointment? それが違いを生む場合、私はバックボーンリレーショナルを使用しています。

4

2 に答える 2

2

フォームのテキストを変更した後、モデルが更新されていない場合は、データを明示的に更新する必要があります

SampleView = Backbone.View.extend({
el: '#formEl',
events : {
    "change input" :"changed",
    "change select" :"changed"
},

initialize: function () {
    _.bindAll(this, "changed");
},
changed:function(evt) {
   var changed = evt.currentTarget;
   var value = $("#"+changed.id).val();
   var obj = "{\""+changed.id +"\":\""+value+"\"";

   // Add Logic to change the client phone
   if (changed.id === 'client_phone') {
          obj = obj + "client\":{\"phone\":\""+value+"\""};
   }
   obj = obj + "}";
   var objInst = JSON.parse(obj);
   this.model.set(objInst);            
}
});

参照 : 手動でぼかしイベントを追跡せずに、フォーム入力を Backbone.js のモデルにバインドできますか?

機種変更イベントにもバインド可能

    model.on('change:client_phone', 
       function () { 
           //Set client value here  
       });
于 2012-07-11T05:27:49.513 に答える
1

私はそれをこのように動作させることができました:

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
    )   
于 2012-07-19T17:28:52.263 に答える