【问题标题】:JSViews computed observable linked to <select> not updating with .set()JSViews 计算的 observable 链接到 <select> 不使用 .set() 更新
【发布时间】:2016-01-20 15:22:54
【问题描述】:

我正在尝试将&lt;select&gt; 框链接到 JSViews 中计算的可观察对象。 &lt;select&gt; 在依赖变量更改时更新良好。但是,当在&lt;select&gt; 框中选择一个新值时,它不会触发我定义的 set() 函数。见https://jsfiddle.net/4y274h0L/3/

模板

<select>
    {{for options}}
        <option value="{{:#data}}" data-link="{:#data} selected{:~root.computedSelectedOption() == #data}"></option>
    {{/for}}
</select>

计算可观测

function privateComputedOption() {
    return vm.tempSelectedOption;
}

privateComputedOption.depends = ["~root.tempSelectedOption"];

privateComputedOption.set = function(val) {
    $.observable(vm).setProperty("tempSelectedOption", val);
    console.log(val);
};

非常欢迎任何帮助!

【问题讨论】:

    标签: jsviews computed-observable


    【解决方案1】:

    您实际上并没有将 select 数据链接到计算的 observable。对于双向绑定,当用户更改下拉选择时,select 上的绑定将调用 setter。

    <select data-link="computedSelectedOption()">
      {{for options}}
        <option value="{{:#data}}"
         data-link="{:#data} selected{:~root.computedSelectedOption()==#data}">  
        </option>
      {{/for}}
    </select>
    
    {^{:computedSelectedOption()}}
    

    我更新了上面的jsfiddle:

    https://jsfiddle.net/4y274h0L/4/

    参见例如http://www.jsviews.com/#samples/form-els/array-binding。

    顺便说一句,在 JsViews 的下一次更新中,事情将被简化,您不需要在选项上将数据链接到 selected{:~root.computedSelectedOption()==#data}"。只需选择 &lt;select data-link="computedSelectedOption()"&gt; 上的数据链接就足够了。

    【讨论】:

    • 谢谢!确实是这样。在实际代码中,我仍然使用对非计算值的旧引用。所以我在选择中确实有一个数据链接,但它是另一个视图模型属性。
    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2019-06-02
    • 2017-01-16
    • 2014-11-01
    • 1970-01-01
    相关资源
    最近更新 更多