All the training videos I watch you only see very basic function and viewmodels structures defined to only show the topic explained. With large applications this is not always possible.
I am fairly new to javascript and KnockoutJs and need your feedback on what approach is best and what works and what does not.
I want to know how you structure your viewmodels and javascript files when building web apps. What is the best way of doing this for maintainability and best practices?
Here is a basic example I'll use with the Revealing Module Pattern.
var app = app || {};
app.PersonViewmodel = (function () {
var self = this;
self._persons = ko.observableArray([]);
return {
persons: self._persons
};
})();
ko.applyBindings(app.PersonViewmodel);
This is what will be in myapp.js.
Some additional questions I have:
- Do I drop the above deceleration of viewmodels inside of my
$(function() {});? - Do I create observable properties in the viewmodel that I want to use for flagging such as IsVisible or IsClicked? Although these properties has nothing to do with the person viewmodel, it is still needed for behaviour on the view?
- How do I pass in and handle one viewmodel to another?
- Is it recommended to work with you viewmodel inside a standard js function()? Or do you need to keep all operations of the viewmodel isolated inside itself?
- Do you create a seperate .js file in your project for each viewmodel?
Any feedback with some examples would be greatly appreciated. Again, I apologize for the beginner questions.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire