Skip to content

Commit

Permalink
fix(): add the reject function at the expected errorIndex position in…
Browse files Browse the repository at this point in the history
… the args array (#436)

We don't want that the reject cb takes the position of an optional
argument that has not been defined
For example the Dialogs.alert method takes an optional 'buttonLabel'
string. In case we do not set this value, and thus want to use the
default value, the 'reject'
callback get spliced into this position due the fact that the splice
start index is bigger than the array length.
Dialogs.alert("title", "message", "My button text") --> args =
["title",  resolve, "message", "My button text", reject]
Dialogs.alert("title", "message") -->  args = ["title", resolve,
"message", reject] --> reject is on the position of the buttontitle!

The cordova-plugin-dialogs alert function receives the wrong arguments
—> alert: function(message, completeCallback, title, buttonLabel)
The buttonLabel will receive the "reject" callback instead of a
undefined value.
  • Loading branch information
kevinboosten authored and ihadeed committed Aug 17, 2016
1 parent 95e2562 commit 4e87ac7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ function setIndex(args: any[], opts: any = {}, resolve?: Function, reject?: Func
} else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
// If we've specified a success/error index
args.splice(opts.successIndex, 0, resolve);
args.splice(opts.errorIndex, 0, reject);

// We don't want that the reject cb gets spliced into the position of an optional argument that has not been defined and thus causing non expected behaviour.
if (opts.errorIndex > args.length) {
args[opts.errorIndex] = reject; //insert the reject fn at the correct specific index
} else {
args.splice(opts.errorIndex, 0, reject); //otherwise just splice it into the array
}
} else {
// Otherwise, let's tack them on to the end of the argument list
// which is 90% of cases
Expand Down

0 comments on commit 4e87ac7

Please sign in to comment.