Backbone.js 带主干的嵌套属性模板
我有一个Backbone.js 带主干的嵌套属性模板,backbone.js,backbone-relational,Backbone.js,Backbone Relational,我有一个预约模型,每个实例都有一个客户端。以下是我编辑约会的模板: <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 %>" placeh
预约
模型,每个实例都有一个客户端
。以下是我编辑约会的模板:
<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\u phone
从555-555-5555
更改为555-555-1234
,但JSON对象中的client
对象的电话号码没有更改。我需要改变一下那个电话号码
如何使这些字段(如电话号码字段)实际“接受”,以便将它们作为
客户机对象的一部分传递给服务器,并位于约会下,而不是直接位于约会下?如果有区别,我使用的是主干关系型。如果在更改表单文本后,您的模型没有被更新,那么您需要显式地更新数据
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);
}
});
参考:
您还可以绑定到模型更改事件
model.on('change:client_phone',
function () {
//Set client value here
});
我能让它像这样工作:
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
)
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
)