0
votes

I would like to create a page for inserting a new car. I am using knockout.js and want to use knockout mapping. The problem is that for create, in order to get a structure for the mapping, I need to make an ajax call and return new and empty serialized object. Is there any to prevent this ajax call?
(I also don't want to write down the whole model structure by myself)

1

1 Answers

1
votes

If you don't want to create a JavaScript version of your object and you don't want to do an ajax call to get it, it's not possible that Knockout magically knows the structure of your object.

What you could do, is use Fiddler to intercept the AJAX call. Fiddler will then show you the JSON representation of your object. You can copy paste this into your JavaScript so you have a 'cached' version of your model for first use. Pass this to Knockout and Knockout will create the object for you.

Of course the cached version will get out of sync with your server model (the same can happen with a plain JavaScript model). This is just something you have to deal with manually.

A way around this is to use a T4 template to do some code generation at compile time. Trough reflection you can generate a .js file with the up to date version of the model.