繁体   English   中英

使用jQuery.val()更新Ember TextField的值

[英]Update the value of Ember TextField using jQuery.val()

我认为这些行:

 //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吗?

您可以使用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');
    },
}

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总是很不错的。

您不应该使用jQuery。

您可以做的是将内容中的单个项目传递给increase动作,并在动作中增加其价值。

<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>

在您的控制器中:

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

示例Jsbin ,用于您的用例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM