I use jQuery promises to pre-populate a parameter value select. This works great, but my question is what to do if the Ajax call is not really required, so the code does not return a promise.
The code below works fine, if this.orgIdnot undefined. But when it is not, I get an error message:
getFormValues: function() {
var _this = this;
return _this.prefillOrgs()
.then(function(orgId) {
_this.globalOptions.orgId = orgId;
return true;
});
},
prefillOrgs: function() {
if (typeof this.orgId !== 'undefined') {
return $.ajax({
type: 'GET',
url: '/api/1.0/org_code?exact=true&q=' + this.orgId,
dataType: 'json',
context: this
});
} else {
return [];
}
}
Error returned:
Uncaught TypeError: _this.prefillOrgs(...).then is not a function
I assume this is because returning is []not a promise and does not have a specific method .then(). However, I would like to return []as my payload in this case.
How can I get around this? I would prefer not to make an unnecessary Ajax call if possible.
: , this .