2

我正在创建一个包含帐单和送货地址字段的表单。当用户选中“相同地址”时,我想在帐单地址和送货地址之间创建绑定;当未选中该框时,我想断开该绑定。

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, 以及其他一些没有运气的方法。我已经能够将运费设置为计费,但没有绑定,模板也没有更新。

任何帮助或朝着正确方向轻推将不胜感激!

4

1 回答 1

5

我不是 Ember 专家,但似乎您需要使用Ember.Binding。也许是这样的:

Ember.oneWay(App.Shipping, "firstName", "App.Billing.firstName");

App.Shipping.firstName在和App.Billing.firstName字段之间设置一种方式绑定。

http://jsfiddle.net/mSxeP/1/

于 2013-05-14T21:27:03.427 回答