Event handlers are within the controller file for your view. You can delete the implementation of the functions onInit
and onErrorCall
, as you will implement new logics:
Add the following code to the onInit
function:
this.getView().addStyleClass("sapUiSizeCompact"); // make everything inside this View appear in Compact mode
var oConfig = this.getOwnerComponent().getModel("config");
var userName = oConfig.getProperty("/UserName");
var userModel = this.getOwnerComponent().getModel("userModel");
var oTable = this.getView().byId("userTable");
oTable.setModel(userModel);
Add a function called callUserService
:
callUserService: function() {
var oModel = this.getOwnerComponent().getModel("userModel");
var result = this.getView().getModel().getData();
var oEntry = {};
oEntry.UserId = "0000000000";
oEntry.FirstName = result.FirstName;
oEntry.LastName = result.LastName;
oEntry.Email = result.Email;
oModel.setHeaders({
"content-type": "application/json;charset=utf-8"
});
var mParams = {};
mParams.success = function() {
sap.m.MessageToast.show("Create successful");
};
mParams.error = this.onErrorCall;
oModel.create("/Users", oEntry, mParams);
},
Take a look at the code you have just added. You can see it is creating a JSON object with the service fields. Add a new function called callUserUpdate
callUserUpdate: function() {
var oModel = this.getOwnerComponent().getModel("userModel");
oModel.setHeaders({
"content-type": "application/json;charset=utf-8"
});
var mParams = {};
mParams.error = function() {
sap.m.MessageToast.show("Update failed");
};
mParams.success = function() {
sap.m.MessageToast.show("Update successful");
};
oModel.submitChanges(mParams);
},
Finally, here is the code for the error handling function:
onErrorCall: function(oError) {
if (oError.statusCode === 500 || oError.statusCode === 400 || oError.statusCode === "500" || oError.statusCode === "400") {
var errorRes = JSON.parse(oError.responseText);
if (!errorRes.error.innererror) {
sap.m.MessageBox.alert(errorRes.error.message.value);
} else {
if (!errorRes.error.innererror.message) {
sap.m.MessageBox.alert(errorRes.error.innererror.toString());
} else {
sap.m.MessageBox.alert(errorRes.error.innererror.message);
}
}
return;
} else {
sap.m.MessageBox.alert(oError.response.statusText);
return;
}
},
Keep the reusable methods from the original script.