0

这里的两个函数都返回“未定义”。我不知道有什么问题.. 看起来很直接??

在控制器中,我设置了一些属性来向用户显示一个空的文本字段,以确保他们输入自己的数据。

Amber.ProductController = Ember.ObjectController.extend ({
    quantity_property: "",
    location_property: "",
    employee_name_property: "",

//quantitySubtract: function() {
//return this.get('quantity') -= this.get('quantity_property');
//}.property('quantity', 'quantity_property')

  quantitySubtract: Ember.computed('quantity', 'quantity_property', function() {
    return this.get('quantity') - this.get('quantity_property');
  });
});

在路线中,正在设置员工姓名和位置...

Amber.ProductsUpdateRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('product', params.product_id);
  },
//This defines the actions that we want to expose to the template
  actions: {
    update: function() {
      var product = this.get('currentModel');
      var self = this; //ensures access to the transitionTo method inside the success (Promises) function
  /*  The first parameter to 'then' is the success handler where it transitions
      to the list of products, and the second parameter is our failure handler:
      A function that does nothing.  */
      product.set('employeeName', this.get('controller.employee_name_property'))
      product.set('location', this.get('controller.location_property'))
      product.set('quantity', this.get('controller.quantitySubtract()'))
      product.save().then(
        function() { self.transitionTo('products') },
        function() { }
      );
    }
  }
});

车把上没有特殊的东西

<h1>Produkt Forbrug</h1>
<form {{action "update" on="submit"}}>
   ...
<div>
  <label>
  Antal<br>
  {{input type="text" value=quantity_property}}
  </label>
  {{#each error in errors.quantity}}
    <p class="error">{{error.message}}</p>
  {{/each}}
</div>
<button type="update">Save</button>
</form>
4

2 回答 2

0

get rid of the ()

product.set('quantity', this.get('controller.quantitySubtract'))

And this way was fine:

quantitySubtract: function() {
  return this.get('quantity') - this.get('quantity_property');
}.property('quantity', 'quantity_property')

Update:

Seeing your route, that controller wouldn't be applied to that route, it is just using a generic Ember.ObjectController.

Amber.ProductController would go to the Amber.ProductRoute

Amber.ProductUpdateController would go to the Amber.ProductUpdateRoute

If you want to reuse the controller for both routes just extend the product controller like so.

Amber.ProductController = Ember.ObjectController.extend ({
  quantity_property: "",
  location_property: "",
  employee_name_property: "",

  quantitySubtract: function() {
    return this.get('quantity') - this.get('quantity_property');
  }.property('quantity', 'quantity_property')
});

Amber.ProductUpdateController = Amber.ProductController.extend();
于 2014-10-28T15:58:54.400 回答
0

我最终跳过了这个功能,而是这样做:

product.set('quantity', 
  this.get('controller.quantity') - this.get('controller.quantity_property'))

我仍然不明白为什么我不能使用该功能..我也尝试重命名控制器..但这不是问题..正如前面提到的其他两个值要获取到控制器...

无论如何,感谢您尝试帮助我!

于 2014-10-28T18:04:24.633 回答