Eslint : sap-no-ui5-prop-warning

此生再无相见时 提交于 2020-01-05 06:32:25

问题


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?


回答1:


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.




回答2:


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().



来源:https://stackoverflow.com/questions/51188053/eslint-sap-no-ui5-prop-warning

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!