I want to remove the ESLint warnings.
I got sap-no-ui5-prop-warning
because I used this.getModel().oData["ProductSet('" + sId+ "')"].Matricule
. But when I use the function getData()
, it returns null.
Do you have a suggestion how to fix this?
I want to remove the ESLint warnings.
I got sap-no-ui5-prop-warning
because I used this.getModel().oData["ProductSet('" + sId+ "')"].Matricule
. But when I use the function getData()
, it returns null.
Do you have a suggestion how to fix this?
Look at the docs. It's simple.
/* eslint-disable sap-no-ui5base-prop */
...some code false positives
/* eslint-enable sap-no-ui5base-prop */
Btw getData()
for ODataModel is deprecated.
You can use getProperty("/")
instead. It'll give you the root level of your data.
Actually I use it like this
this.getModel().oData["ProductSet('" + sId+ "')"].Matricule
Please do not disable the ESLint setting sap-no-ui5-prop-warning
for cases like above. The way how the property is accessed goes highly against the conventions and JS coding guidelines.
JavaScript Code Issues
Don't use methods or properties that are not public
Don't use or override "private" methods or properties. (...) Always double check in the API Reference. If UI5 changes the implementation in a future release, your code will break if you fail to follow this guideline.
In order to access the value properly, make use of available APIs - preferably those returning context such as createBindingContext
[api] or getBindingContext
[api]:
createProductContext: function(productId /*, ...*/) {
const myODataModel = /*...*/;
const path = myODataModel.createKey("/ProductSet", {
ProductID: productId, // See https://stackoverflow.com/a/47016070/5846045
});
myODataModel.createBindingContext(path, this.handleProductContext.bind(this));
},
handleProductContext: function(context) {
if (!context) {
return; // error - Context couldn't be created
}
const matricule = context.getProperty("Matricule"); // <-- Access property value
// ...
},
If the binding context is already available:
someMethod: function() {
const context = this.byId("thatControlHavingODataBound").getBindingContext(/*modelName*/);
this.handleProductContext(context);
},
Accessing property via a context ensures that the data is already available on the client-side in contrast to myModel.getProperty()
.