Ember js:将两个数相乘得到总数

时间:2014-12-04 19:37:40

标签: javascript ember.js

构建迷你报价应用程序。 现在不使用Ember数据或其他任何东西,只是尝试使用原始数据。

似乎无法将两个数字相乘以获得特定项目的总数。尝试将费率和小时相乘以获得项目总金额。

我的报价控制器和项目控制器:

Quoter.QuoteController = Ember.ObjectController.extend({
actions: {
    addSection: function(){
        var sectionTitle = "Section 3"
        var new_section = {sectionTitle: sectionTitle, items: []};
        quote.sections.pushObject(new_section);
    },
    addItem: function(section){
        var name = "Item"
        var hours = 20
        var total = 100
        var new_item = {title: name, hours: hours, total: total};
        section.items.pushObject(new_item);
    },
    pmRatio: function(){
        ratio = this.get('ratio');

    },
    removeSection: function(section){
    quote.sections.removeObject(section);
},
    removeItem: function(section, item){
    section.items.removeObject(item);
},
}    

});

Quoter.ItemController = Ember.ObjectController.extend({
    needs: ["quote"],
    getTotal: function(item) {
        return this.get('rate') * this.get('item.hours')
    }.observes('rate', 'item.hours'),
})

我的数据:

var quote =
{
    id: 1,
    title: "Project",
    customer: "Customer",
    rate: 105,
    pm_ratio: 0.2,
    sections: [
    {
        sectionTitle: "Section 1",
        items: [
        {
            title: "Item 1",
            hours: 10,
            total: 100
        },
        {
            title: "Item 2",
            hours: 6,
            total: 100

        }]
    },
    {
        sectionTitle: "Section 2",
        items: [
        {
            title: "Item 1",
            hours: 10,
            total: 100

        },
        {
            title: "Item 2",
            hours: 6,
            total: 100
        }]
    }]
}

我的模板:

<script type="text/x-handlebars" data-template-name="quote">
<div class="container">
  <div class="row">
    <h1>Quote</h1>


    <h2>{{title}}</h2>
    {{input id="title" class="form-control" value=title placeholder="Add Project Title"}}

    <h3>{{customer}}</h3>
    {{input id="customer" class="form-control" value=customer placeholder="Add Customer"}}

    <label for="rate">Hourly Rate:</label> 
      {{input type="text" id="rate" class="form-control" value=rate}}

    <label for="ratio">Project Management Ratio:</label> 
    {{input type="text" id="ratio" class="form-control" value=pm_ratio}}


    <button class="btn btn-primary" {{action 'addSection'}}>Add Section</button>
    <div></div>
    {{#each section in sections}}
    <caption>{{section.sectionTitle}}</caption>
    {{input type="text" class="form-control" value=section.sectionTitle}}
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>Item Name</th>
            <th>Hours</th>
            <th>Total</th>
          </tr>
        </thead>
        {{#each item in section.items}}
        <tbody>
          <tr>
            <td>{{input type="text" value=item.title}}</td>
            <td>{{input type="text" value=item.hours}}</td>
            <td>{{getTotal}}</td>

            <td><button {{action 'removeItem' section item}}>Remove Item</button></td>
            {{/each}}
            <td><button {{action 'addItem' section}}>Add Item</button></td>                
            <td>{{totalHours}}</td>
            <td>{{totalCost}}</td>
          </tr>
        </tbody>
        {{/each}}
      </table>
  </div>
</div>

2 个答案:

答案 0 :(得分:2)

我想你的问题是你正在创建一个观察者,而不是一个计算属性。您有以下代码:

getTotal: function(item) {
    return this.get('rate') * this.get('item.hours')
}.observes('rate', 'item.hours')

这会将这些数字相乘,但是Ember不会对结果做任何事情。 (它希望观察到副作用,而不是返回值。)尝试在模板中使用它可能会产生类似[Object object]的东西。您需要将observes更改为property

getTotal: function(item) {
    return this.get('rate') * this.get('item.hours')
}.property('rate', 'item.hours')

答案 1 :(得分:0)

在你的情况下最简单的事情可能就是写一个帮助器,如:

Ember.Handlebars.helper('calculateRate', function(rate, hours) {
  return rate * hours;
});

请参阅完整示例here