敲除如何使用元素获取数据绑定键和值可观察对象? [英] Knockout how to get data-bind keys and value observables using element?
问题描述
我需要使用元素获取数据绑定键和可观察值.
I need to get data bind keys and value observable using element.
<select id="selector" data-bind="options:selectOptions,value:selectedValue"></select>
var ViewModel = {
selectOptions:ko.observableArray([...]),
selectedValue:ko.observable()
...
some other stuff
...
}
在其他视图模型中,我现在可以访问 dom 元素,我需要更新元素的绑定上下文可观察.
In other viewmodel I can access dom element now I need to update element's binding context observable.
如何获取数据绑定键和值?
how can I get data-bind keys and values?
我需要这样的东西
{
options:selectOptions,
value:selectedValue
}
推荐答案
我推荐你使用这个.您可以使用 jquery unobtrusive plugin
在 javascript 端处理它.
Well i recommend you use this. You can handle it on javascript end using jquery unobtrusive plugin
.
http://joel.net/unobtrusive-data-binding-for-淘汰赛-js
为此你可以创建一个对象
For this you can create an object
var binding = {
options: 'tickets',
optionsCaption: "'Choose...'",
optionsText: "'name'",
value: 'chosenTicket'
}
然后像这样使用
$('#tickets').dataBind(binding);
代替这个
<select
data-bind="
options: tickets,
optionsCaption: 'Choose...',
optionsText: 'name',
value: chosenTicket
"
></select>
通过这种方式,您将可以重用 binding
对象,并且您的代码将非常干净.确保在 applyBinding
之前调用它.
this way you will have the binding
object available to reuse and your code will be quite clean. Make sure you call it before applyBinding
.
这篇关于敲除如何使用元素获取数据绑定键和值可观察对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!