0

I defined many custom type in Utils.js: http://plnkr.co/edit/BGqDoWEC7DTmomEFHygd?p=catalogue

I want to add trim() in parseValue

parseValue: function (oValue) {
    if(oValue !== null) {
        return oValue.trim();
    }
    return oValue;
},

Copy and paste this function is kind of dumb, so I want to do sth. like :

trimString : function(oValue) {
    if(oValue !== null) {
        return oValue.trim();
    }
    return oValue;
},

mandatoryValueType : SimpleType.extend("text", {
    formatValue: function (oValue) {
        return oValue;
    },
    parseValue: this.trimString,
    validateValue: function (oValue) {
        if (!oValue) {
            throw new ValidateException(Utils.i18n("MANDATORY_VALIDATE_ERROR"));
        }  
    }
}),

But scope in mandatoryValueType seems can not access to this.trimString, what can I do?

this scope in parseValue function, no trimString function: enter image description here

Another working sample : http://plnkr.co/edit/ahS6NlUHHL0kdvdddvgd?p=preview Reference: https://sapui5.hana.ondemand.com/#/sample/sap.m.sample.InputChecked/preview

Tina Chen
  • 2,010
  • 5
  • 41
  • 73

2 Answers2

2

Maybe you can put the trimString function outside the var Utils object, and set it as a global function.

here is the example: http://plnkr.co/edit/u64DEP0RnT5CJADZyXJg?p=catalogue

edit:

corrected by @boghyon, the trimString function should be a function in a closure, instead of a global function :)

Natalie.Z
  • 93
  • 7
0

in this case the this keyword contains the object which invokes the function: the controller which invokes mandatoryValueType of utils.js.

initialise in utils.js in the upper scope of mandatoryValueType a var that = this. then use inside of mandatoryValueType instead of this.trimString the that.trimString.

edit

self reference to a property of an object in a literal declaration of an object:

var Utils = {
  ...
  trimString: function() {
    ...
  },
  ...
  parseValue: function() {
    this.trimString();
  }
  ..
}
n01dea
  • 1,532
  • 1
  • 16
  • 33
  • http://plnkr.co/edit/yGZxBHsW3FC1u6Pit31I?p=catalogue Added `that` in Utils.js, but still not working... – Tina Chen Dec 13 '17 at 09:34
  • http://plnkr.co/edit/pWqOq60broDI8GovEwQ1?p=preview make a demo in controller.js, but after change `email` input, there will be an error: `this.oType.parseValue is not a function` – Tina Chen Dec 13 '17 at 09:43
  • i guess i misunderstood your question. anyway, please try in the plnkr code sample of the first comment `parseValue: function() {this.trimString}` instead of `parseValue: this.trimString`. or in es6 'get parseValue() {this.trimString}'. – n01dea Dec 13 '17 at 09:45
  • http://plnkr.co/edit/pWqOq60broDI8GovEwQ1?p=preview Updated. `that` scope is `window`... `that.trimString` returned `undefined`. – Tina Chen Dec 13 '17 at 09:48
  • so, is parseValue inside a controller as a method or inside the util object of another module as the controller from which parseValue gets invoked? – n01dea Dec 13 '17 at 09:53
  • Inside `Utils.js`, I used it in controller just for simple demo. – Tina Chen Dec 13 '17 at 09:56
  • This type is use in Master.view.xml. In Master.controller.js, I sap.ui.defined `namespace/util/Utils`, and before `onInit` I add `Utils : Utils` – Tina Chen Dec 13 '17 at 09:58
  • please check this: https://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations?noredirect=1&lq=1. – n01dea Dec 13 '17 at 10:51
  • `this` has changed since `parseValue` is in `SimpleType.extend`, please refer to my update in question. Thanks – Tina Chen Dec 14 '17 at 07:02