我正在創建一個帶有字段的帳單和收貨地址表單。當用戶檢查「相同的地址」時,我想創建帳單地址和送貨地址之間的綁定;當該框未選中時,我想要斷開該綁定。手動連接和斷開綁定
HTML:
{{#with billingAddress}}
{{view Ember.TextField valueBinding="firstName"}}
{{view Ember.TextField valueBinding="lastName"}}
{{/with}}
{{view Ember.Checkbox checkedBinding="view.isSameAddress"}}
{{#with shippingAddress}}
{{view Ember.TextField valueBinding="firstName"}}
{{view Ember.TextField valueBinding="lastName"}}
{{/with}}
的JavaScript:
App.AddressesRoute = Ember.Route.extend({
model: function() {
return {
billingAddress: Checkout.BillingAddress,
shippingAddress: Checkout.ShippingAddress
};
}
});
App.AddressesView = Ember.View.extend({
isSameAddress: false,
useSameAddress: function() {
if (this.isSameAddress) {
// bind billing and shipping addresses
} else {
// remove binding
}
}.observes('isSameAddress')
});
App.Address = Ember.Object.extend({
firstName: null,
lastName: null
});
App.Billing = App.Address.create({
firstName: 'John',
lastName: 'Doe'
});
App.Shipping = App.Address.create({
firstName: '',
lastName: ''
});
我使用Ember.Binding
嘗試,以及一些沒有運氣的其他方法。我已經能夠將運費設置爲結算,但沒有綁定,模板也沒有更新。
任何幫助或在正確的方向微調將不勝感激!
你是如何嘗試使用'Ember.Binding'?你能證明什麼不起作用嗎?或其他什麼方法 – intuitivepixel
這裏是更多信息連接和斷開綁定https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/binding.js#L148 – intuitivepixel