This question has just been raised, so it can update the question with what I did
I solved it by iterating over a window object (or a user-specified root object), and when I found the correct instance, I canceled it and got the name from the index. The final solution can be found here.
https://github.com/AndersMalmgren/Knockout.BindingConventions
End of update
I plan to write a conditional agreement on creating the source code for the template for KnockoutJS / MVC. I started from the small client side of POC and immediately ran into a show stop.
My plan uses this syntax or something like that
MyApp.EditCustomersViewModel = function() { ko.templates.loadView(this); };
At the same time, it checks the tamplate cache or retrieves templates from the server using the name of the object as a key. The problem is that I cannot get the name of the prototype object, I tried this
Object.prototype.getName = function() { var funcNameRegex = /function (.{1,})\(/; var results = (funcNameRegex).exec((this).constructor.toString()); return (results && results.length > 1) ? results[1] : ""; };
If it works for objects defined this way
function MyClass() { }
If you add a prototype to the above object, it will not work, or if you define it as follows
MyApp = {}; MyApp.MyClass = function() { };
The prototype and scope are two points, so this is a show, any ideas?
Fiddle: http://jsfiddle.net/aRWLA/
edit: The background for this is as follows.
On the server, you have a structure like this
- Templates \ [ViewName] \ index.html
- Templates \ [ViewName] \ sub-model-template.html
on the client that you will do
MyApp.EditCustomersViewModel = function() { ko.templates.loadView(this); };
which will generate an ajax request with the name of the object as a key that will retrieve all the templates for the view in question