【问题标题】:Update the value of Ember TextField using jQuery.val()使用 jQuery.val() 更新 Ember TextField 的值
【发布时间】:2013-11-11 08:12:45
【问题描述】:

我的观点是:

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

【问题讨论】:

    标签: javascript jquery ember.js


    【解决方案1】:

    你可以使用 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 应用程序中断,或者在这种情况下无法按预期工作。

    根据您的 cmets 进行编辑。

    您当前的 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 总是好的。

    【讨论】:

    • 谢谢,但文本字段中没有显示任何内容,我收到错误:未捕获错误:路径模型中的对象。项目无法找到或被破坏。 ?
    • 我想知道你的item.qntyitem 是什么?你可以试试上面没有模型前缀的代码。
    • item is: {{#each item in controller.content}} 我是 ember 的新手,正在尝试快速学习它。我已经完成了 ToDo 教程,但还没有理解所有内容,这项工作必须完成! :)
    【解决方案2】:

    你不应该使用 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,用于您的用例。

    【讨论】:

    • 您将在数组控制器中放置项目逻辑。在我看来,这是一种不好的做法。
    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多