From 4e87ac72eabfe4da38124b9025d4b71325999855 Mon Sep 17 00:00:00 2001 From: Kevin Boosten Date: Wed, 17 Aug 2016 13:34:11 +0200 Subject: [PATCH] fix(): add the reject function at the expected errorIndex position in the args array (#436) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/plugins/plugin.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts index a1b2cbc1a..1be9f8508 100644 --- a/src/plugins/plugin.ts +++ b/src/plugins/plugin.ts @@ -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