Reputation: 291
My Model
var oModel = new sap.ui.model.odata.ODataModel("../services/myexp.xsodata", false);
sap.ui.getCore().setModel(oModel,'data');
Now I want to add new record to this model. Simple code -
openUserCreateDialog: function(){
var oUserCreateDialog = new sap.ui.commons.Dialog();
var oSimpleForm1 = new sap.ui.layout.form.SimpleForm({
maxContainerCols: 2,
content:[
new sap.ui.core.Title({text:"Create"}),
new sap.ui.commons.Label({text:"User"}),
new sap.ui.commons.TextField({value:""}),
new sap.ui.commons.Label({text:"Date"}),
new sap.ui.commons.TextField({value:""}),
new sap.ui.commons.Label({text:"Description"}),
new sap.ui.commons.TextField({value:""})
]
});
oUserCreateDialog.addContent(oSimpleForm1);
oUserCreateDialog.addButton(
new sap.ui.commons.Button({
text: "Submit",
press: function() {
var content = oSimpleForm1.getContent();
var oEntry = {};
oEntry.User = content[2].getValue();
oEntry.Date = content[4].getValue();
oEntry.Description = content[6].getValue();
sap.ui.getCore().getModel().create('data>/user', oEntry, null, function(){
oUserCreateDialog.close();
sap.ui.getCore().getModel().refresh();
},function(){
oUserCreateDialog.close();
alert("Create failed");
}
);
}
})
);
oUserCreateDialog.open();
},
When I am submitting the form, It's throwing an error as-
Uncaught TypeError: sap.ui.getCore(...).getModel(...).create is not a function
What is wrong with my code. Please help. Thank you
Upvotes: 0
Views: 4141
Reputation: 849
The problem with your code is you are setting the model as a named model but accessing the default model.
sap.ui.getCore().setModel(oModel,'data');
The above code sets oModel with the name 'data' but while reading you are accessing the default model
sap.ui.getCore().getModel().create('data>/user', oEntry, null, function(){
oUserCreateDialog.close();
sap.ui.getCore().getModel().refresh();
},function(){
oUserCreateDialog.close();
alert("Create failed");
}
);
So while accessing the model call the named model
sap.ui.getCore().getModel('data').create('/user', oEntry, null, function(){
oUserCreateDialog.close();
sap.ui.getCore('data').getModel().refresh();
},function(){
oUserCreateDialog.close();
alert("Create failed");
}
);
and this will work.
Upvotes: 1