将viewmodel绑定到knockout中属性的存在

时间:2014-07-16 16:01:33

标签: javascript html5 knockout.js details

我正在使用Knockout.js来填充一组HTML5 <details>元素。结构如下:

<div class="items" data-bind="foreach: Playlists">
    <details class="playlist-details" data-bind="attr: {id: 'playlist-details-' + $index()}">
        <summary>
            <span data-bind="text: name"></span> - <span data-bind="text: count"></span> item(s)
            <div class="pull-right">
                <button data-bind="click: $parent.play, css: {disabled: count() == 0}, attr: {title: playbtn_title}" class="btn"><i class="icon-play"></i> Play</button>
                <button data-bind="click: $parent.deleteList" class="btn btn-danger"><i class="icon-trash"></i> Delete</button>
            </div>
        </summary>
        <div class="list" data-bind="with: items" style="padding-top: 2px;">
            ...
        </div>
    </details>
</div>

ViewModel中的数据如下所示:

var VM = {
    Playlists: [
        {
            name: "My Playlist1",
            count: 3,
            items: [<LIST OF SONG ID'S>],
            playbtn_title: "Play this playlist"
        },
        {
            name: "My Playlist2",
            count: 5,
            items: [<LIST OF SONG ID'S>],
            playbtn_title: "Play this playlist"
        },
        {
            name: "My Playlist3",
            count: 0,
            items: [],
            playbtn_title: "You need to add items to this list before you can play it!"
        }
    ]
};

我想添加记住详细信息视图的open or closed state的功能。我以前使用jQuerylocalStorage 1 实现了这种行为,但对于这个项目,我想原生使用Knockout而不是使用jQuery。

我已向ViewModel中的播放列表添加了isOpen属性,该页面加载时会从localStorage检索。但是,我似乎无法在Knockout中使用attr绑定,因为HTML5 spec表示仅查找open属性的presence or absence,而不是值

当ViewModel的open属性发生变化时,如何让Knockout添加和删除<details>元素的isOpen属性?


1 :像这样:

// On the initial page load.
contents += '<details ' + ((localStorage['tl_open_playlist-details-' + counter] == 1) ? 'open' : '') ' class="playlist-details" id="playlist-details-' + counter + '" data-name="' + escape(listname) + '">'

...

// Update storage when things are clicked.
$(document).on('DOMSubtreeModified', 'details.playlist-details', function() {
    if ($(this).prop('open')) {
        localStorage['tl_open_' + this.id] = 1;
    } else {
        delete localStorage['tl_open_' + this.id];
    }
});

1 个答案:

答案 0 :(得分:0)

您可以修改attr绑定以考虑另一个绑定选项(此处名为attrRemoveWhenFalse)并为您删除该属性:

<input class='testInput' type="text" 
       data-bind="attr: { disabled: isDisabled }, attrRemoveWhenFalse: true" />

var originalAttr = { init: ko.bindingHandlers.attr.init, 
                     update: ko.bindingHandlers.attr.update }
ko.bindingHandlers.attr.update = function (element, valueAccessor, 
                                           allBindingAccessor, viewModel, 
                                           bindingContext) {
    if (typeof originalAttr.update === 'function') 
        originalAttr.update(element, valueAccessor, allBindingAccessor, 
                            viewModel, bindingContext);
    if (allBindingAccessor().attrRemoveWhenFalse) {
        for (var prop in valueAccessor()) {
            if (!ko.utils.unwrapObservable(valueAccessor()[prop])) {
                element.removeAttribute(prop);
            }
        }
    }
}

Demo