在我的视图中有以下几行:
//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吗?
发布于 2013-11-11 16:32:42
您可以使用jQuery。但我认为您忽略了数据绑定的概念。
您使用item.qnty属性为TextField创建了一个值绑定。
您的递增函数将如下所示:
actions: {
increase: function() {
var quantity = this.get('model.item.qnty');
this.set('model.item.qnty', quantity++);
},
}您甚至可以使用快捷方式函数:
actions: {
increase: function() {
this.increaseProperty('model.item.qnty');
},
}TextField将自动检测到item.qnty已更改,并更新Ember中的值。
您不应该使用Ember框架以外的任何其他方法来更新Ember值。这样做可能会导致Ember应用程序崩溃,或者在这种情况下,无法按预期工作。
根据您的评论进行编辑。
您当前的哈佛商学院:
{{#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总是很好的。
https://stackoverflow.com/questions/19901618
复制相似问题