Ember-rails:为我的计算值返回'undefined'的函数



这里的两个函数都返回'undefined'。我搞不清出了什么问题。。看起来很直接??

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

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>

清除()

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

这种方式很好:

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

更新:

看到您的路由,该控制器不会应用于该路由,它只是使用一个通用的Ember.ObjectController

Amber.ProductController将转到Amber.ProductRoute

Amber.ProductUpdateController将转到Amber.ProductUpdateRoute

如果你想在两条路线上重复使用控制器,只需像这样扩展产品控制器

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();

我最终跳过了这个函数,而是这样做:

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

我仍然不明白为什么我不能使用那个函数。。我还试图重命名控制器。。但这不是问题所在。。如前所述,要获取到控制器的其他两个值。。。

不管怎样,谢谢你帮我!

最新更新