diff --git a/example/www/js/controllers.js b/example/www/js/controllers.js index aaea13c..3f3d5cb 100644 --- a/example/www/js/controllers.js +++ b/example/www/js/controllers.js @@ -12,6 +12,10 @@ angular.module('starter.controllers', []) return angular.isDefined($scope.step3.something); }; + $scope.showMessage = function() { + alert('Thanks!'); + }; + }]) .controller('DashCtrl', function($scope) {}) diff --git a/example/www/templates/intro.html b/example/www/templates/intro.html index de7a65a..aef9cfb 100644 --- a/example/www/templates/intro.html +++ b/example/www/templates/intro.html @@ -161,7 +161,7 @@

This slide is scrollable

- +

Thanks for running the wizard!

diff --git a/src/ion-wizard.js b/src/ion-wizard.js index 6b41de4..c96379d 100644 --- a/src/ion-wizard.js +++ b/src/ion-wizard.js @@ -6,7 +6,8 @@ angular.module('ionic.wizard', []) return{ restrict: 'EA', controller: [function() { - var conditions = []; + var conditions = [], + actions = []; this.addCondition = function(condition) { conditions.push(condition); @@ -28,6 +29,14 @@ angular.module('ionic.wizard', []) : conditions[index].prev(); }; + this.addStepAction = function(action) { + actions.push(action); + }; + + this.performStepAction = function(index) { + angular.isDefined(actions[index]) ? actions[index]() : undefined; + }; + }], link: function (scope, element, attrs, controller) { var currentIndex = 0; @@ -37,9 +46,11 @@ angular.module('ionic.wizard', []) element.css('height', '100%'); scope.$on("wizard:Previous", function() { + controller.performStepAction(currentIndex - 1); $ionicSlideBoxDelegate.previous(); }); scope.$on("wizard:Next", function() { + controller.performStepAction(currentIndex + 1); $ionicSlideBoxDelegate.next(); }); @@ -54,6 +65,8 @@ angular.module('ionic.wizard', []) scope.$on("slideBox.slideChanged", function(e, index) { currentIndex = index; }); + + controller.performStepAction(0); } } @@ -63,7 +76,8 @@ angular.module('ionic.wizard', []) restrict: 'EA', scope: { nextConditionFn: '&nextCondition', - prevConditionFn: "&prevCondition" + prevConditionFn: "&prevCondition", + actionFn: '&stepAction' }, require: '^^ionWizard', link: function(scope, element, attrs, controller) { @@ -86,6 +100,7 @@ angular.module('ionic.wizard', []) }; controller.addCondition(conditions); + controller.addStepAction(attrs.stepAction ? scope.actionFn : undefined); } } }]) diff --git a/tests/ion-wizard_test.js b/tests/ion-wizard_test.js index f82390e..64d3e92 100644 --- a/tests/ion-wizard_test.js +++ b/tests/ion-wizard_test.js @@ -243,6 +243,49 @@ describe('Unit testing wizard directives', function() { }); }); + describe("step-action", function() { + var firstStepActionPeformed, + secondStepActionPerformed; + beforeEach(function () { + secondStepActionPerformed = false; + firstStepActionPeformed = false; + inject(function(_$ionicSlideBoxDelegate_) { + scope.secondStepActionFn = function () { + secondStepActionPerformed = true; + }; + scope.firstStepActionFn = function () { + firstStepActionPeformed = true; + }; + element = angular.element("
Move next
\ +
Move next
\ +
Move Next
\ +
"); + + $compile(element)(scope); + scope.$digest(); + controller = element.controller('ionWizard'); + }); + }); + + it("should perform a step action on the first step when first loading", function () { + expect(firstStepActionPeformed).toBe(true); + }); + + + it("should perform a step action when moving forward to that step", function () { + expect(secondStepActionPerformed).toBe(false); + $rootScope.$broadcast("wizard:Next"); + expect(secondStepActionPerformed).toBe(true); + }); + + it("should perform a step action when moving back to that step", function () { + expect(secondStepActionPerformed).toBe(false); + $rootScope.$broadcast("slideBox.slideChanged", 2); + $rootScope.$broadcast("wizard:Previous"); + expect(secondStepActionPerformed).toBe(true); + }); + + }); }); describe('Test wizard start button', function() {