I’m trying to “clean up” the architecture of my jQuery Mobile application by using Knockout.js.
What’s the “best practice” way to initialize the view model objects and bind them to the view? Should I be doing it similar to the following?
$("#myPage").live("pagecreate", function (event, ui) {
ko.applyBindings(new App.ViewModels.MyViewModel(), this);
});
This seems like it would create the view model as the page was created. Are there any edge cases I’m missing where this will be a problem? If so, what’s the recommended style?
I want to get this right from the start, as I figure it’ll be a debugging nightmare if I have a misunderstanding of how these bits fit together.
applyBindingsseems a little more magical than it actually is. The only thing you have to be sure of is that all your elements are actually loaded before calling it.