Is there a concept in SAPUI5 similar to angular services? I need to inject a custom service (basically a class with some methods) to another custom class, and I was expecting that the framework would automatically instantiate my custom service, but when I try to use it it's a constructor and not the object. The code, pruned by not necessary details, is here:
The custom service:
sap.ui.define([
"sap/ui/base/Object",
"ArA/model/productionOrdersDefinition/Order"
], function(Object, Order) {
"use strict";
return Object.extend("ArA.model.productionOrdersDefinition.InputProvider", {
constructor: function() {
},
onInit: function(){
this.order = new Order();
this.order.id = "0000133535";
// ...
},
getOrder: function(){
// ...
return this.order;
}
});
});
The object using the custom service:
sap.ui.define([
"ArA/model/productionOrdersDefinition/BaseObject",
"ArA/model/productionOrdersDefinition/Order",
"ArA/model/productionOrdersDefinition/InputProvider"
], function(BaseObject, Order, InputProvider) {
"use strict";
return BaseObject.extend("ArA.model.productionOrdersDefinition.ManagerProgrammazione", {
constructor: function() {
BaseObject.call(this);
// this InputProvider is a constructor (method) and getOrder is not a function
var order = InputProvider.getOrder();
// ...
}
});
});
Looks like that you try to transfer you experience and concepts from Angular to UI5. But it's better to do it in a way that was designed in UI5 world :)
There is a concept of models, which are aimed to basically deal with data. You can create your own model just as you did - an extension of base UI5 object or JSONModel. Most probably you are not going to use OData protocol for network communication so you will store you data in JSONModel.
Declare domain-specific methods in your model(s) and instantiate them in your controllers directly or in the application's component (if you want to have a kind of global model available for all controllers). You can get the instance of component from any controller via getOwnerComponent method.
Another approach to organize your data models - is to create a special kind of object called "BO" - Business Object, this BO will handle all domain-specific affairs (instantiate needed models, do the networking, data manipulation and so on). So you would need to create this BO in your controller and call needed high-level methods on it from controller once user-actions are triggered.
In addition, you will have to take care of destruction of your models/BOs in controller or component lifecycle methods.