javascript - Search an observable array using Knockout, Breeze, Durandal -
i have following observable array, gets data webapi using breeze when view activated (at activate() function on durandal)
var prices = ko.observablearray();
the price class has 3 attributes: id, name , price.
i have following array:
var services = [{ active: true, name: 'service1', price: getprice('service1') }, { active: true, name: 'service2', price: getprice('service2') }];
what getprice(name) function should object has name passed parameter...
i want able following on view:
<div class="services"> <ul data-bind="foreach: services"> <li data-bind="visible: active, text: name + ' ($' + price.price + ')'"></li> </ul> </div>
i've searched lot on stackoverflow , tried in many ways, haven't been able make work. i'm not sure if should use ko.utils.arrayfirst(), ko.computed(), or should do.. i've tried many approaches without success.
update figured i'd add answer mryellow in comments in case comes across this.
results = ko.utils.arraymap(inputs, function(item) { return new modelfoo(item); });
original
you can't use data in view because isn't observable array, nor properties observable.
var services = ko.observablearray([{ active: ko.observable(true), name: ko.observable('service1'), // option 1 price: ko.observable(getprice('service1')) }, { active: ko.observable(true), name: ko.observable('service2'), // option 2 price: ko.computed(getprice(name())) }]);
this work in view. if trying breeze should have observable unless going wrong, @ point need bit more code.
if want iterate through prices , make services -
var services = ko.observablearray(); ko.utils.arrayforeach(prices(), function (price) { services.push(new service(price.active(), price.name(), price.price()); });
with model somewhere
function service(active, name, price) { var self = this; self.active = ko.observable(active); self.name = ko.observable(name); self.price = ko.computed(getprice(name)); }
the reason why if going create new knockout objects(observables) need iterate through breeze results , make them. use model type shown can efficient , keep within scope.
another option if prices() has want besides price property, create constructor method when breeze entities returned compute price.
Comments
Post a Comment