javascript - Filtering a collection of view models in using Knockback.js -
i creating page using knockback.js displays employee's tasks in table. have view model task, contains boolean observable called isselected. view model employee contains collection observable of collection of task view models called 'tasks'.
i want add together attribute/function/observable called 'selectedtasks' exposes selected tasks. want meet next requirements:
both 'tasks' , 'selectedtasks' should give me view models, not models. when add together model original tasks collection, 'tasks' observable should updated. when user selects newly added model, 'selectedtasks' should updated well. there should 1 view model every task model. otherwise might view model states task x not selected while view model states x selected.to demonstrate bit more clearly, created jsfiddle: http://jsfiddle.net/drojoke/cg6d88lp/14/
so far, managed working 'tasks' attribute using collection observable, seen here:
this.tasks = kb.collectionobservable(tasks, { view_model: function (task) { homecoming new taskviewmodel(task); } });
i'm using view_model
alternative turn every task in tasks collection taskviewmodel
. when add together new task tasks collection, collectionobservable
updated exptected.
i tried create selectedtasks
attribute collectionobservable
filter option, so:
this.selectedtasks = kb.collectionobservable(tasks, { view_model: function (task) { homecoming new taskviewmodel(task); }, filters: function (task) { homecoming task.isselected(); // isselected undefined. } });
but unfortunately, object gets passed filters function not taskviewmodel
, task object, have no access isselected
observable.
i couldn't find lot filtering collection of view models instead of models. how can created selectedtasks observable filters view models , doesn't create additional view models?
you can utilize computed observables:
this.selectedtasks = ko.computed(function() { homecoming this.tasks().filter(function(task) { homecoming task.isselected(); }); }, this);
javascript knockout.js collections knockback.js
No comments:
Post a Comment