I have a web project where I need to model some basic JavaScript classes and put them in a separate javascript file. Now I want to use them locally on the page and add them to the main view model, which acts as an anchor object. My question is, how do you understand the relationship between the model and the main view model?
This is the class model from api:
Namespace.Problem = function()
{
var self = this;
self.identifier = ko.observable();
self.summary = ko.observable();
self.title = ko.observable();
};
Namespace.Problem.withJson = function(json)
{
var problem = new Namespace.Problem();
problem.identifier(json.identifier);
problem.summary(json.summary);
problem.title(json.title);
return problem;
};
and here is the main view model:
function MasterViewModel()
{
var self = this;
self.problem = ko.observable({});
self.loadData = function()
{
self.problem(Namespace.Problem.withJson(json));
}
}
I leave the applyBindings function outside the code here.
This is the correct way to have an attribute in the main view model that looks like this above, or should be like
self.problem = new Namespace.Problem();
Are the api class model properties set correctly? Or is the following better?
self.identifier;
self.summary;
self.title