【问题标题】:ValueBinding not working on adding new Object in my Ember viewValueBinding 无法在我的 Ember 视图中添加新对象
【发布时间】:2014-10-24 13:20:12
【问题描述】:

我有一个输入字段类型编号,其中显示两个值相乘的结果。

我已经在 html 中设置了我的 valueBinding 的总函数

这是我的 Ember.View.extend 与总计算

App.TotalView = Ember.View.extend({
 templateName: 'total',
 tagName: 'input',
 attributeBindings: ['total:value', 'placeholder', 'type'],
 placeholder: null,
 type: 'number',
 total: (function() {
    var res= parseInt(this.get('controller.newThread.selectContentTariffa')) * parseInt(this.get('controller.newThread.primary'));
    return isNaN(res)?"":res;
 }).property('controller.newThread.selectContentTariffa', 'controller.newThread.primary')
});

我已经定义了对我的 NewThread 的调用

 newThread:function(){
    return {selectContentTariffa:null,primary:null,total:null,weight:null};
 }.property(),

这是我选择计算值时的 html:valueBinding=newThread.selectContentTariffa, valueBinding=newThread.primary and valueBinding=newThread.total for the total result

    <tr>
      <td>{{view Ember.Select 
                 prompt="Tariffa" 
                 valueBinding=newThread.selectContentTariffa
                 content=selectContentTariffa
                 optionValuePath="content.value"
                 optionLabelPath="content.label"}}
      </td>
      <td>{{view Em.TextField
                 type="number"
                 valueBinding=newThread.primary
                 class="form-control"}}
      </td>
      <td>{{view "total" valueBinding=newThread.total}}</td>
      <td><button class="btn btn-success" {{action add item}}>Add</button></td>
    </tr>

  <script type="text/x-handlebars" data-template-name="total">
    {{view.total valueBinding=newThread.total}}
  </script>

这是我添加后显示的值的我的 html

{{#each item in model}}        
    <tr>
          <td>{{item.selectContentTariffa}}</td>
          <td>{{item.primary}}</td>
          <td>{{item.total}}</td>
        </tr>
{{/each}}

我不知道我错过了什么,我在这里重现了这个问题:http://emberjs.jsbin.com/qakado/13/edit?html 你可以在未绑定下的最后一列中看到结果的值

【问题讨论】:

    标签: javascript model-view-controller ember.js jsfiddle jsbin


    【解决方案1】:

    分配给newThread 属性的对象的total 属性永远不会随结果一起设置。因此,一种方法可以是使总属性成为计算属性并在那里进行乘法运算。然后在视图中显示该属性。

    http://emberjs.jsbin.com/vivohiyefu/1/edit?html,js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多