knockout.js - KO data-binding "text:" to a function with a parameter -
is possible in way following without have pass through temporary observables / computed observables? have common data used lookups display data:
<span data-bind="text: lookupcontactname(31)"></span>
result: function exectuded, parameter found, logic ok, , computed full name returned, not displayed (probably since no observable)
<span data-bind="text: lookupcontactname(contactid)"></span>
result: contactid not parsed correct parameter value not visible.
i suppose work correctly need create custom bindings?
in general: started doubting bit whether or not approach load data once , try match id's approach. views? i'm better of creating joined db tables / views / sp's?
thanks, j.
here, relevant code pieces i'm using... learn jsfiddle future help.
all alert's returning expected values... still text data-binding doesn't receive value
a common data library:
customers.contact = function () { var self = this; id = ko.observable(); title = ko.observable(); givenname = ko.observable(); surname = ko.observable(); fullname = ko.computed(function () { return title()+". "+givenname()+" "+surname(); }); return { id: id, title: title, givenname: givenname, surname: surname, fullname: fullname }; }; customers.contactlist = function () { var self = this; contactlist = ko.observablearray([]); //.publishon("contactlist"); loadcontactdata = function () { var self = this; customers.helperdataservice.getcontactdata(loadcontactdatacallback); }; loadcontactdatacallback = function (json) { var self = this; $.each(json, function (i, p) { var contact = new customers.contact().id(p.id) .title(p.title) .givenname(p.name) .surname(p.surname); contactlist.push(contact); }); }; lookupcontactname = function (id) { var self = this; alert("value found: "+id); ko.utils.arrayforeach(contactlist(), function (contact) { alert("search: contactid: " + contact.id() + " - " + "id: " + id); if (contact.id() === id) { alert("found: contactid: " + contact.id() + " - " + "id: " + id); alert("value:" + contact.fullname()); return contact.fullname(); } }); }; return { loadcontactdata: loadcontactdata, lookupcontactname: lookupcontactname }; };
which called here... (i changed contactid() , passes correct value function)
<div>creator: <span data-bind="text: lookupcontactname(contactid())"></span></div>
if <span data-bind="text: lookupcontactname(contactid())"></span>
in foreach loop need prepend method $root or else "lookupcontactname" within array looping trough.
try:
<div>creator: <span data-bind="text: $root.lookupcontactname(contactid())"></span></div>
Comments
Post a Comment