带挖空的选定对象图案
有谁知道我如何使用 knockoutJS 完成以下模式? 我一直在 apache flex 中使用这种模式,想看看我是否可以模仿它。 我不确定如何用另一个可观察值替换。非常感谢任何帮助/想法。
//模型
myViewModel = {
items : ko.observableArray(),
selected_item : ko.observable()
}
//视图
<h3 data-bind="text : myViewModel.selected_item.name"> </h3>
<ul>
<!-- ko foreach: myViewModel.items -->
<li data-bind="text : name"/>
<!-- /ko -->
</ul>
//逻辑
$('li').click(function(e){
//check ko.dataFor(this) has different id from that of myViewModel.selected_item
//if id different
//set myViewModel.selected_item to ko.dataFor(this)
//rejoice as h3 text changes
})
Does anybody know how I'd accomplish the following pattern with knockoutJS?
I use this pattern all the time in apache flex and want to see if I can mimic it.
I am unsure how to replace an observable with another. Any help/thoughts much appreciated.
//model
myViewModel = {
items : ko.observableArray(),
selected_item : ko.observable()
}
//view
<h3 data-bind="text : myViewModel.selected_item.name"> </h3>
<ul>
<!-- ko foreach: myViewModel.items -->
<li data-bind="text : name"/>
<!-- /ko -->
</ul>
//logic
$('li').click(function(e){
//check ko.dataFor(this) has different id from that of myViewModel.selected_item
//if id different
//set myViewModel.selected_item to ko.dataFor(this)
//rejoice as h3 text changes
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你走在正确的轨道上。您可以使用几种模式来选择所选项目。如果您想像上面那样不显眼地附加一个单击处理程序,那么最好的选择是使用委托处理程序,以便您可以处理对 observableArray 的更改。委托处理程序将能够处理添加的新元素。
因此,您可以执行以下操作:
这是一个示例: http://jsfiddle.net/rniemeyer/hBUBN/
当您绑定到您的
h3
,因为selected_item
可以为 null,您需要通过将其包装在with
块中来保护自己(在示例中),调用一个函数来处理null,或者在绑定中执行此操作,例如 (data-bind="text: myViewModel.selected_item() ? myViewModel.selected_item().id : 'unknown'"
)。为了保持简洁,可以将此逻辑放入一个函数中,您可以从数据绑定中调用该函数或使用with
防止出现问题(尽管当它为空时它不会呈现任何内容) 。否则,您甚至可以这样做:
KO 2.0 中的
click
(和event
)绑定将当前数据作为第一个参数传递。您可以使用$parent
变量访问上一级范围(或使用$root
访问基础级别)。可观察量是函数,您可以通过将新值作为第一个参数传递来设置它们的值。因此,在这里执行$parent.selected_item
将调用可观察函数,将数据作为第一个参数传递。因此,它会将您选择的值设置为正确的项目。示例:http://jsfiddle.net/rniemeyer/gemug/
You are on the right track. There are a few patterns that you can use to choose a selected item. If you want to attach a click handler unobtrusively, as you have above, then your best bet would be to use a delegated handler, so that you are set to handle changes to your observableArray. A delegated handler will be able to handle new elements being added.
So, you could do something like:
Here is a sample: http://jsfiddle.net/rniemeyer/hBUBN/
When you bind to your
h3
, sinceselected_item
can be null you would need to protect yourself by wrapping it in awith
block (in the example), calling a function that handles null, or doing it in the binding like (data-bind="text: myViewModel.selected_item() ? myViewModel.selected_item().id : 'unknown'"
). To keep it clean, this logic can be put in a function and you can call the function from your data-bind or usingwith
prevents this from being an issue (although it renders nothing when it is null).Otherwise, you can even just do this:
The
click
(andevent
) binding in KO 2.0 pass the current data as the first argument. You can use the$parent
variable to access one scope level up (or$root
to get to the base level). Observables are functions and you set their value by passing the new value as the first argument. So, doing$parent.selected_item
here will call the observable function passing your data as the first argument. So, it will set your selected value to the proper item.Sample: http://jsfiddle.net/rniemeyer/gemug/