Skip to content

Commit f9f6e65

Browse files
committed
Adding hooks for 'beforeNext' and 'beforeBack'
1 parent 2aca2fd commit f9f6e65

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ The plugin accepts options as a single object argument. Supported options are:
2727
* **backType** HTML input type for back button (default: "reset")
2828
* **nextClasses** class(es) for next button (default: "btn btn-primary")
2929
* **backClasses** class(es) for back button (default: "btn")
30+
* **beforeNext** function to call before next step (allowing for validation -- return false to prevent next)
3031
* **onNext** function to call on next step
32+
* **beforeBack** function to call before back step (allowing for validation -- return false to prevent back)
3133
* **onBack** function to call on back up
3234
* **onInit** a chance to hook initialization
3335
* **onDestroy** a chance to hook destruction

src/acc-wizard.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,32 @@
232232
.click(function(ev) {
233233
ev.preventDefault();
234234
var panel = $(this).parents(".accordion-body")[0];
235-
var next = "#" + $(".accordion-body",
236-
$(panel).parents(".accordion-group")
237-
.next(".accordion-group")[0])[0].id;
238-
$(next).collapse("show");
239-
hook('onNext', panel);
235+
var resp = hook('beforeNext', panel);
236+
if(resp) {
237+
var next = "#" + $(".accordion-body",
238+
$(panel).parents(".accordion-group")
239+
.next(".accordion-group")[0])[0].id;
240+
$(next).collapse("show");
241+
hook('onNext', panel);
242+
}
240243
});
241-
244+
242245
// When the user clicks the "Back" button in
243246
// any panel, retrurn to the previous panel.
244-
247+
245248
$("."+options.stepClass,$el)
246249
.children("button[type='"+options.backType+"']")
247250
.click(function(ev) {
248251
ev.preventDefault();
249252
var panel = $(this).parents(".accordion-body")[0];
250-
var prev = "#" + $(".accordion-body",
251-
$(panel).parents(".accordion-group")
252-
.prev(".accordion-group")[0])[0].id;
253-
$(prev).collapse("show");
254-
hook('onPrev', panel);
253+
var resp = hook('beforeBack', panel);
254+
if(resp) {
255+
var prev = "#" + $(".accordion-body",
256+
$(panel).parents(".accordion-group")
257+
.prev(".accordion-group")[0])[0].id;
258+
$(prev).collapse("show");
259+
hook('onPrev', panel);
260+
}
255261
});
256262
}
257263

@@ -282,14 +288,14 @@
282288
// hookName: function() {}
283289
// Then somewhere in the plugin trigger the callback:
284290
// hook('hookName');
285-
291+
286292
function hook(hookName) {
287293
if (options[hookName] !== undefined) {
288294
// Call the user defined function.
289295
// Scope is set to the jQuery element we are operating on.
290296
var fn = options[hookName];
291297
arguments[0] = el;
292-
fn.apply(this, arguments);
298+
return fn.apply(this, arguments);
293299
}
294300
}
295301

0 commit comments

Comments
 (0)