I have a function that uses case to return an object
function GetModel(mymodel){
var mydatamodel = null;
switch(mymodel)
{
case 'Contact':
mydatamodel = kendo.data.Model.define({
id: "__KEY",
fields: {
__KEY: { type: "string" },
__STAMP: { type: "number" },
ID: { editable: false, nullable: true },
firstName: { type: "string" },
middleName: { type: "string" },
lastName: { type: "string" }
}
});
break;
case 'Address':
mydatamodel = kendo.data.Model.define({
id: "__KEY",
fields: {
__KEY: { type: "string" },
__STAMP: { type: "number" },
ID: { editable: false, nullable: true },
street: { type: "string" }
}
});
break;
case 'ContactType':
mydatamodel = kendo.data.Model.define({
id: "__KEY",
fields: {
__KEY: { type: "string" },
__STAMP: { type: "number" },
ID: { editable: false, nullable: true },
name: { type: "string" }
}
});
break;
};
return mydatamodel
};
//Try to create new instance
var mymodel = new GetModel("Contact")
Now this function does return an object but the object is not the same as
using the below code to create a new object which is what I need
var mydatamodel = kendo.data.Model.define({
id: "__KEY",
fields: {
__KEY: { type: "string" },
__STAMP: { type: "number" },
ID: { editable: false, nullable: true },
firstName: { type: "string" },
middleName: { type: "string" },
lastName: { type: "string" }
}
});
//Create new object;
var mymodel = new mydatamodel();
This works as expected and creates a new instance of the kendoui.datamodel where
the function returns the kendoui.data.modal…. as a function and not a new instance of the object
I would like to be able to call a switch function because I want this code in a different .js file then the main .html page for code seperation. So how would
I go about getting the above function GetModel to act in the same way as the
var = kendoui.data.model… is working
Thanks,
Dan
Don’t use the new operator on your function.
Just make the function return an instance like this:
And use your creator function like this:
EDIT
Or use you code like this: