Reputation: 109
Hi there i'm building multipage application with Knockout.js and i'm facing a problem writing too much duplicated code..
As example in one of my views i have:
var personVm = function {
//properties here
}
personVm.prototype {
//extensions & methods here
}
And at some point i have to use the same ViewModel in some other view.. So i end up duplicating code..
I searched for solutions and require.js is the most common.. But in my case require.js is not suitable and i can't use it. So i will be glad if some one shares some ways to deal with this problem.
Update:
To clear more my question i need some kind of container from which i can grab instances of given ViewModel for my differen views ( which i get from asp.net )
Upvotes: 1
Views: 293
Reputation: 204
Why can't you just keep personVm in a file which you can access from all over and make a new one each time?
thisModel.person = new personVm(data);
otherModel.person = new personVm(data);
Or am I not understanding the problem correctly?
Upvotes: 0
Reputation: 38529
You could use knockouts own ko.utils.extend
var personVm = function {
//properties here
}
personVm.prototype {
//extensions & methods here
}
then, later
var superHeroVm = function() {
var self = this;
self.specialPower = ko.observable();
ko.utils.extend(self, new personVm());
}
Upvotes: 1