Skip to content Skip to sidebar Skip to footer

Pass A Function That Returns The Value Of The Ko.computed

I have two functions generate and test. When the user clicks a button generate is called which then calls test function. I receive the following error: Pass a function that return

Solution 1:

Without seeing all your code this is hard to be sure, but if the code (as is) works when directly included in generate, then I'd be willing to bet that this is not what you think it is in the test function. this has a different meaning inside the test function than it did inside generate.

Fire up Chrome or Firebug and set a breakpoint on the var unmapped = ko.mapping.toJS(this); line. Run your program and when the breakpoint hits go to the Console and look at this. Is it your ViewModel?

If this is what you expect inside generate you could always call test like this:

test.apply(this);

This will explicitly set the context of this for the method invocation.

Another option is to set a self variable inside your ViewModel. This is usually done at the top and looks like: var self = this;. By creating this variable you can then reference self' inside any functions within the outer "function" scope, and not have to worry about the value ofthis` fluctuating.

--

Here is a simple fiddle to simulate what I think is happening: http://jsfiddle.net/jearles/aLFWe/

Open Chrome or Firebug console and look at the logged objects. Note that Window was the logged object when I just called updatea(), but was Object in the original click function and when I called updateb.apply(this) or updatec() (which referenced self).

Post a Comment for "Pass A Function That Returns The Value Of The Ko.computed"