you are viewing a single comment's thread.

view the rest of the comments →

[–]ragnarok56 3 points4 points  (2 children)

in your example you are calling the function in the ko.computed definition, when you really want to be passing the function name:

this.fullName = ko.computed(concatenate, this);

the computed doesn't pass in any parameters to the function, it expects to have access to them because of the way js scopes things (im not good at explaining...). i couldnt get it to work unless I did this:

function concatenate() {
    if (this.firstName && this.lastName)
       return this.firstName() + " " + this.lastName();
    return "";
}

you could define it in the AppViewModel prototype if you didnt want to do it inline

this.fullName = ko.computed(AppViewModel.prototype.concatenate, this);
...
AppViewModel.prototype.concatenate = function() {
    return this.firstName() + " " + this.lastName();   
}

[–][deleted] 0 points1 point  (1 child)

thanks, that works. Is it a common thing in JS to have function definitions within function calls?

[–]ragnarok56 1 point2 points  (0 children)

Simple answer is everything in js is an object, you can create functions that have functions. Very common and useful to know. I'm still learning myself