Using jquerymobile and knockout, I am trying to create a custom binding that looks as follows
<ul data-bind="listview: observablearray">
<li data-bind="text: text"></li>
</ul>
where the listview binding would first act like a foreach binding and then apply $(element).listview() to it (or $(element).listview('refresh') on an update).
I tried doing the following to naively imitate the foreach binding:
ko.bindingHandlers.listview = {
init: function (element, valueAccessor) {
var listview = $(element);
listview.listview();
},
update: function (element, valueAccessor) {
var listview = $(element);
setTimeout(function () {
listview.html('');
var items = valueAccessor().list;
var link = valueAccessor().link;
var text = valueAccessor().text;
var icon = valueAccessor().icon;
$.each(ko.utils.unwrapObservable(items) || [], function (i, item) {
var li = $('<li></li>').css({
height: '44px'
})
var a;
if (link) {
a = $('<a href="#"></a>').click(function () {
link(item)
});
li.append(a);
}
if (icon) {
(a || li).append($('<img />').attr('src', icon(item)).addClass('ui-li-icon'));
}
if (text) {
(a || li).append($('<span></span>').text(item[text]));
}
listview.append(li);
});
listview.listview('refresh')
}, 0);
}
};
Unfortunately, not only was this a poor implementation that didn't use templating, it also through an error on init:
Uncaught TypeError: Cannot read property 'jQuery19101983379740267992' of undefined
Thanks in advance!