Not seeing all your code, it's hard to understand, but if the code (as it is) works when it is directly included in the generated file, then I bet that this is not what you think test in it. this has a different meaning inside the test function than inside generate .
Launch Chrome or Firebug and set a breakpoint in the line var unmapped = ko.mapping.toJS(this); . Run the program and when the breakpoint goes to the console, look at this . Is this your viewmodel?
If this is what you expect inside generate , you can always call test like this:
test.apply(this);
This explicitly sets the this context for the method call.
Another option is to set the self variable inside your ViewModel. This is usually done from above and looks like this: var self = this; . By creating this variable, you can refer to self' inside any functions within the outer "function" scope, and not have to worry about the value of this`, wavering.
-
Here is a simple script to simulate what I think is happening: http://jsfiddle.net/jearles/aLFWe/
Open the Chrome or Firebug console and look at the registered objects. Note that Window was a registered object when I just called updatea() , but there was Object in the original click function and when I called updateb.apply(this) or updatec() (which referenced self ).
source share