首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember-rails:函数为我的计算值返回'undefined‘

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

Stack Overflow用户
提问于 2014-10-28 23:44:29
回答 2查看 197关注 0票数 0

这里的两个函数都返回'undefined‘。我不知道问题出在哪里。看起来很直白??

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

代码语言:javascript
复制
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');
  });
});

在路线中,正在设置employeeName和位置...

代码语言:javascript
复制
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() { }
      );
    }
  }
});

车把上没有什么特别的东西

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2014-10-28 23:58:54

去掉()

代码语言:javascript
复制
product.set('quantity', this.get('controller.quantitySubtract'))

这种方式很好:

代码语言:javascript
复制
quantitySubtract: function() {
  return this.get('quantity') - this.get('quantity_property');
}.property('quantity', 'quantity_property')

更新:

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

Amber.ProductController会去Amber.ProductRoute

Amber.ProductUpdateController会去Amber.ProductUpdateRoute

如果您想在两个路径上重用控制器,只需像这样扩展产品控制器即可。

代码语言:javascript
复制
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();
票数 0
EN

Stack Overflow用户

发布于 2014-10-29 02:04:25

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

代码语言:javascript
复制
product.set('quantity', 
  this.get('controller.quantity') - this.get('controller.quantity_property'))

我还是不明白为什么我不能使用那个函数..我还试着重命名控制器..但这不是问题所在..如前所述,要提取到控制器的另外两个值...

无论如何,谢谢你试着帮助我!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26612559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档