带挖空的选定对象图案

发布于 2024-12-25 20:13:52 字数 728 浏览 0 评论 0原文

有谁知道我如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2025-01-01 20:13:52

你走在正确的轨道上。您可以使用几种模式来选择所选项目。如果您想像上面那样不显眼地附加一个单击处理程序,那么最好的选择是使用委托处理程序,以便您可以处理对 observableArray 的更改。委托处理程序将能够处理添加的新元素。

因此,您可以执行以下操作:

$("#content").on("click", "li", function(e) {
    var item = ko.dataFor(this),
        current = myViewModel.selected_item;

    if (!current() || item.id !== current().id) {
        current(item);
    }
});

这是一个示例: http://jsfiddle.net/rniemeyer/hBUBN/

当您绑定到您的 h3,因为 selected_item 可以为 null,您需要通过将其包装在 with 块中来保护自己(在示例中),调用一个函数来处理null,或者在绑定中执行此操作,例如 (data-bind="text: myViewModel.selected_item() ? myViewModel.selected_item().id : 'unknown'")。为了保持简洁,可以将此逻辑放入一个函数中,您可以从数据绑定中调用该函数或使用 with 防止出现问题(尽管当它为空时它不会呈现任何内容) 。

否则,您甚至可以这样做:

<!-- ko foreach: myViewModel.items -->
    <li  data-bind="text : name, click: $parent.selected_item"></li>
<!-- /ko -->

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:

$("#content").on("click", "li", function(e) {
    var item = ko.dataFor(this),
        current = myViewModel.selected_item;

    if (!current() || item.id !== current().id) {
        current(item);
    }
});

Here is a sample: http://jsfiddle.net/rniemeyer/hBUBN/

When you bind to your h3, since selected_item can be null you would need to protect yourself by wrapping it in a with 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 using with prevents this from being an issue (although it renders nothing when it is null).

Otherwise, you can even just do this:

<!-- ko foreach: myViewModel.items -->
    <li  data-bind="text : name, click: $parent.selected_item"></li>
<!-- /ko -->

The click (and event) 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文