How to access other function in UI5 custom control/type? - sapui5

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:
Another working sample : http://plnkr.co/edit/ahS6NlUHHL0kdvdddvgd?p=preview
Reference: https://sapui5.hana.ondemand.com/#/sample/sap.m.sample.InputChecked/preview

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 :)

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

Related

The createClass helper of babeljs make mangle not working

I am using babel as transpiler and I want to mangle some methods with uglifyjs.
Here is a demo:
class A {
methodA() {}
}
And its output by babel:
var A = function () {
function A() {
_classCallCheck(this, A);
}
_createClass(A, [{
key: "methodA",
value: function methodA() {}
}]);
return A;
}();
However when I try to mangle methodA, it does not work. Because methodA in the output is a string.
But the same code output by typescript works, it is not a string:
var A = /** #class */ (function () {
function A() {
}
A.prototype.methodA = function () { };
return A;
}());
So my question is: How can I mangle method name when using babeljs ?
OK, I found the answer.
Just use loose mode:
[ ['#babel/preset-env', { loose: true }] ]
The result will be closer to TS.

Using Scopes on the Load method not working

Is there a way to abstract this Closure to my MerchantUser model in a way similar to using scopes on with()?
So far I have this which works:
$merchant_user->load(['permissions' => function ($query) use ($merchantId) {
if ($merchantId) {
$query->where('merchant_user_permission.merchant_id','=', $merchantId);
}
}]);
But I'd like to do something like this:
$merchant_user->loadPermissions($merchantId);
In my Model:
public function scopeLoadPermissions($query, $merchantId = null)
{
return $query->load(['permissions' => function ($q) use ($merchantId) {
if ($merchantId) {
$q->where('merchant_user_permission.merchant_id','=', $merchantId);
}
}]);
}
Which at the moment just returns an error:
"Method Illuminate\Database\Query\Builder::load does not exist."
For this case you dont need add scope. Instead if you can add this function in your model
public function loadPermissions($merchantId = null)
{
return $this->load(['permissions' => function ($q) use ($merchantId) {
if ($merchantId) {
$q->where('merchant_user_permission.merchant_id','=', $merchantId);
}
}]);
}
and usage
$merchant_user->loadPermissions($merchantId);

Is it possible to apply destructuring to storing a class method in a variable?

I have the following render method that calls another method in its class:
render() {
const isValidField = this.isValidField();
}
Is it possible - and if so is it a good idea - to use destructuring in this case to avoid repeating the method name in the variable?
I'm looking for something like this:
render() {
const { isValidField }() = this;
}
You could use a getter function:
get isValidField() {
return true;
}
render() {
const {isValidField} = this;
}

winjs.xhr and function call in winjs

Scenario: I am trying to call a webservice which returns the results in json, the logic should be very straight forward.
I call a webservice url in WinJS.xhr() add a then function to process the result, here i am trying to bind it to a list.
I am using the below but i am not getting anything displayed what am i doing wrong here?
Can some one tell me how to call a winjs.xhr() from a function and return some object which i can bind as i am trying below?
function getData() {
return WinJS.xhr({ url: "http://search.twitter.com/search.json?q=%23windows8&rpp=10" })
}
function myFunc() {
getData().then(function (xhr) {
var jsondata = JSON.parse(xhr.responseText)
return jsondata;
// ...do something with the data when it arrives...
}, function (err) {
// ...do something with the error
});
}
var dataList = new WinJS.Binding.List(myFunc());
//var dataList = new WinJS.Binding.List(dataArray);
var publicMembers =
{
itemList: dataList
};
WinJS.Namespace.define("DataExample", publicMembers);
The ctor for WinJS.Binding.List accepts a list or array as the initial contents of the list. Your myFunc() will return nothing. You can set up an empty list using
var dataList = new WinJS.Binding.List()
and export it as you currently do.
Then, in myFunc(), placed below the dataList declaration, you can just add items to the list. For example, assuming that jsondata parses into an array:
function myFunc() {
getData().then(function (xhr) {
var jsondata = JSON.parse(xhr.responseText)
jsondata.forEach(function(entry) { dataList.push(entry); });
}, function (err) {
// ...do something with the error
});
}
Edit: I also assume you have bound dataList.dataSource to the itemDataSource of a WinJS.UI.ListView and set a matching itemTemplate property or render function.

Mootools Class - Calling a function within a function

I'm learning Mootools classes at the moment and there's something that I can't seem to get my head around or find a decent example.
Basically, I need to be able to call a function within a different function of the same class; example below:
var Bob = new Class({
initialize: function () {
this.message = 'Hello';
},
someOther: function() {
this.message2 = 'Bob';
},
getMessage: function() {
return this.someOther();
},
});
window.addEvent('domready', function() {
var map = new Bob;
alert(map.getMessage());
});
From this code, I would have thought that the alert would produce 'Bob' which has been set in the function 'someOther' but it's outputting a undefined message.
Can anyone help or point out where I'm going wrong?
Thanks in advance,
er not quite.
someOther has no return value in itself, it's a setter. you invoke it and it will set this.message2 into the class but it returns nothing. methods should return this (the instance, so making it chainable) or a value, when a getter.
anyway, you can make it set the property and return it like so:
var Bob = new Class({
initialize: function() {
this.message = 'Hello';
},
someOther: function() {
return this.message2 = 'Bob'; //bad
},
getMessage: function() {
return this.someOther(); // why
},
});
window.addEvent('domready', function() {
var map = new Bob;
alert(map.getMessage());
alert(map.message2); // bob
});
though, semantically, you want to have 1 getter. .getMessage should just return this.message - you can write a different method that calls someOther and returns it.
have a look at this pattern for a getter/setter in a class context I wrote the other day:
http://fragged.org/using-overloadsetter-overloadgetter-to-make-flexible-functions-in-mootools_1451.html
etc etc. for more help, look at the keetology blogs or davidwalsh.name - or the mootorial - plenty of examples of class use and structure.
most key ones are listed here: https://stackoverflow.com/tags/mootools/info