I have a long list of items that I want to show in a <ul>
. I want to add a "filter" input, so the user can narrow down the list of items to those matching the filter.
My controller contains a filter
prop and a list
array:
function Ctrl() {
this.filter = m.prop('');
this.list = [];
}
I've added an update
method to the controller, which looks at the filter
prop and updates the contents of the list
array:
Ctrl.prototype.update = function (value) {
var _this = this;
if (this.filter()) {
searchItems(this.filter(), function (items) {
_this.list = items;
});
} else {
this.list = [];
}
};
Finally, my view iterates over the list
array and renders the items. Additionally, it displays an input on top, bound to the filter
prop:
var view = function (ctrl) {
return m('#content', [
m('input', {
oninput: m.withAttr("value", ctrl.filter),
value: ctrl.filter()
}),
m('ul', [
ctrl.list.map(function (item, idx) {
return m('li', m('span', item.getName()));
})
])
]);
};
My question is, how to make the update
function fire when the value of filter
changes, so that I get the updated list of items?
Do I need to position two oninput
events? One to update filter
and one to fire update
?
Should I use a single oninput
event and update the filter
property within he update
function?
Anything else?