1

在我看来,我有以下几行:

 //this code is within a {{#each item in controller.content}} so id will not be unique
 //so by giving it just an id like i have is not going to work 
 {{#each item in controller.content}}
   <div class="pull-right" id="qnty-bulk">{{view Ember.TextField class="span1 qnty-bulk" id="qnty-bulk" valueBinding="item.qnty" type="number" min="1" max="9999999" disabled=true}}</div>
   <button class="pull-right" {{ action "increase" }}>
       Up
   </button>
 {{/each}}

在我的控制器中,我有动作

 actions: {
    increase: function() {
        var inputField = $("#qnty-bulk");  //how can I access the Ember.TextField here for each of my items in the {{#each item in controller.content}}??
        var inputValue = inputField.val();
        inputValue = (inputValue != null && inputValue != '' ? (isNaN(inputValue) ? 0 : inputValue) : 0);
        inputValue++;
        console.log(inputField.val());
        inputField.val(inputValue);
    },

我想每次单击向上按钮时将文本字段的值增加 1,我该怎么做?我可以使用 jquery 吗?

4

2 回答 2

1

你可以使用 jQuery。但是我认为您缺少数据绑定的概念。

TextField您为使用该item.qnty属性进行了值绑定。

你的增加函数看起来像这样:

actions: {
    increase: function() {
        var quantity = this.get('model.item.qnty');
        this.set('model.item.qnty', quantity++);
    },
}

您甚至可以使用快捷功能:

actions: {
    increase: function() {
        this.increaseProperty('model.item.qnty');
    },
}

Ember 将自动检测item.qnty已更改并更新 TextField 中的值。

除了 Ember 框架之外,您永远不应该使用任何其他方式来更新您的 Ember 值。这样做可能会导致您的 Ember 应用程序中断,或者在这种情况下,无法按预期工作。

编辑,根据您的评论。

您当前的 hbs:

{{#each item in controller}}
    <div {{action increase}} ></div>
{{/each}}

这将触发increase阵列控制器中的功能,您想在其中编辑阵列中的项目。

让我们为您的项目指定一个项目控制器:

{{#each item in controller itemController='myItem'}}
    <div {{action increase}} ></div>
{{/each}}

你的 MyItemController:

App.MyItemController = Ember.ObjectController.extend({

    actions: {
        increase: function(){
            this.increaseProperty('model.qnty');
        }
    }
})

这将触发increase项目控制器中的功能,您可以在其中直接访问您的项目。为您的数组设置一个 ArrayController 并为该数组中的项目设置一个 ObjectController 总是好的。

于 2013-11-11T08:32:41.843 回答
0

You shouldn't use jQuery.

What you can do is pass the individual item in your content to the increase action and increase its value inside the action.

<div class="pull-right">
 {{input value=item.qnty type="number" min="1" max="9999999" disabled=true}}
</div>
<button class="pull-right" {{ action "increase" this}}>
 Up
</button>

And in your controller:

actions: {
increase: function(item) {
    var qnty = Ember.get(item,'qnty');
    Ember.set(item,'qnty',++qnty);
 }
}

A sample Jsbin, for your use case.

于 2013-11-11T09:06:23.417 回答