gpt4 book ai didi

ember.js - 使用对象作为选项时,如何设置 Ember.js 选择 View 的值?

转载 作者:行者123 更新时间:2023-12-02 20:02:46 24 4
gpt4 key购买 nike

当使用对象而不是字符串时,我在弄清楚如何让 Ember 的选择 View 更新 dom 时遇到了一些麻烦。这个例子的行为符合我的预期:

http://jsfiddle.net/XZ9b7/

仅使用字符串数组作为值,通过单击按钮将绑定(bind)属性的值设置为新值将自动更新 dom 并将下拉列表设置为相同的值。

但是,当在此示例中使用对象时:

http://jsfiddle.net/pRBKt/

我尝试将绑定(bind)属性的值设置为 id、值和选项对象本身的副本,但我无法让它影响选择 View 。

我只需要一种在使用对象作为选项时设置选择 View 的值的方法

最佳答案

我在你的 fiddle 中添加了另一个按钮:

<div><button onclick="App.someObject.set('letterSelection', App.someOptions.objectAt(0));">real a</button></div>

http://jsfiddle.net/Sly7/BYjk6/#base

我认为你必须使用相同的对象,但不能使用副本。因此,在您的情况下,您必须从绑定(bind)到 Ember.Select 的 content 属性的数组中选取对象。

编辑:jsfiddle 使用#each 和 {{action}}。我认为它清理了模板和代码。

http://jsfiddle.net/Sly7/sCr8T/

<script type="text/x-handlebars">
{{view Ember.Select
contentBinding="App.someOptions"
selectionBinding="App.someObject.letterSelection"
optionLabelPath="content.val"
optionValuePath="content.id"
}}
{{App.someObject.letterSelection.val}}
{{#each option in App.someOptions}}
<div>
<button {{action "updateSelection" target="App.someObject" context="option"}}>{{option.val}}</button>
</div>
{{/each}}
</script>

JavaScript:

window.App = Ember.Application.create();

App.someObject= Ember.Object.create({
title: "Some Title",
letterSelection: null,

updateSelection: function(event){
var newSelection = event.context;
this.set('letterSelection', newSelection);
}
});

App.someOptions = [
{'id':'id_a','val':'a'},
{'id':'id_b','val':'b'},
{'id':'id_c','val':'c'},
{'id':'id_d','val':'d'}
];

关于ember.js - 使用对象作为选项时,如何设置 Ember.js 选择 View 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11551443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com