From 0f7b853410fca8fa6282a96f098e2171f58db38e Mon Sep 17 00:00:00 2001 From: Tobias Kopelke Date: Fri, 10 Apr 2015 10:20:42 +0200 Subject: [PATCH 1/2] added code to handle ui-sref-on attribute @see https://github.com/angular-ui/ui-router/issues/1863 --- src/stateDirectives.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/stateDirectives.js b/src/stateDirectives.js index cc8359612..950dc1aef 100644 --- a/src/stateDirectives.js +++ b/src/stateDirectives.js @@ -134,7 +134,12 @@ function $StateRefDirective($state, $timeout) { if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) { // HACK: This is to allow ng-clicks to be processed before the transition is initiated: var transition = $timeout(function() { - $state.go(ref.state, params, options); + var $transition = $state.go(ref.state, params, options); + if($transition && attrs.uiSrefOn) { + scope.$eval(attrs.uiSrefOn, { + $transition: $transition + }); + } }); e.preventDefault(); From b036055251e48137bb1317c8ef2b7cd63ed68b47 Mon Sep 17 00:00:00 2001 From: Tobias Kopelke Date: Fri, 10 Apr 2015 10:35:27 +0200 Subject: [PATCH 2/2] added ui-sref-on test Test will set an ui-sref-on event-handler and trigger it, it will then check if the resolve parameter triggers correctly --- test/stateDirectivesSpec.js | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/test/stateDirectivesSpec.js b/test/stateDirectivesSpec.js index 90bed508b..98e995698 100644 --- a/test/stateDirectivesSpec.js +++ b/test/stateDirectivesSpec.js @@ -559,3 +559,96 @@ describe('uiView controllers or onEnter handlers', function() { expect(count).toBe(1); })); }); + + +ddescribe('uiSrefResolve', function() { + var template = '
A
'; + + beforeEach(module('ui.router')); + + var $timeout, defer; + + beforeEach(module(function($stateProvider) { + $stateProvider.state('top', { + url: '' + }).state('a', { + url: '/a', + template: 'Success', + controller: function(a) {}, // require a + resolve: { + a: function($q) { + defer = $q.defer(); + return defer.promise; + } + } + }); + })); + + beforeEach(inject(function(_$timeout_) { + $timeout = _$timeout_; + })); + + function triggerClick(el, options) { + options = angular.extend({ + metaKey: false, + ctrlKey: false, + shiftKey: false, + altKey: false, + button: 0 + }, options || {}); + + var e = document.createEvent("MouseEvents"); + e.initMouseEvent( + "click", // typeArg of type DOMString, Specifies the event type. + true, // canBubbleArg of type boolean, Specifies whether or not the event can bubble. + true, // cancelableArg of type boolean, Specifies whether or not the event's default action can be prevented. + undefined, // viewArg of type views::AbstractView, Specifies the Event's AbstractView. + 0, // detailArg of type long, Specifies the Event's mouse click count. + 0, // screenXArg of type long, Specifies the Event's screen x coordinate + 0, // screenYArg of type long, Specifies the Event's screen y coordinate + 0, // clientXArg of type long, Specifies the Event's client x coordinate + 0, // clientYArg of type long, Specifies the Event's client y coordinate + options.ctrlKey, // ctrlKeyArg of type boolean, Specifies whether or not control key was depressed during the Event. + options.altKey, // altKeyArg of type boolean, Specifies whether or not alt key was depressed during the Event. + options.shiftKey, // shiftKeyArg of type boolean, Specifies whether or not shift key was depressed during the Event. + options.metaKey, // metaKeyArg of type boolean, Specifies whether or not meta key was depressed during the Event. + options.button, // buttonArg of type unsigned short, Specifies the Event's mouse button. + null // relatedTargetArg of type EventTarget + ); + el[0].dispatchEvent(e); + } + + it('should execute ui-sref-on code on click', inject(function($rootScope, $q, $compile, $state) { + el = angular.element(template); + template = $compile(el)($rootScope); + $rootScope.$digest(); + + var a = angular.element(template[0].querySelector('.a')); + + var state = 'start', transition = null; + $rootScope.check = function(_transition) { + state = 'resolving'; + transition = _transition; + + transition.then(function() { + state = 'resolved'; + }); + }; + + expect(state).toBe('start'); + expect(transition).toBeNull(); + expect(a.attr('class')).toBe('a'); + + triggerClick(a); + $timeout.flush(); + + expect(state).toBe('resolving'); + expect(transition).not.toBeNull(); + + defer.resolve('done'); + $timeout.flush(); + + expect(state).toBe('resolved'); + })); + +}); \ No newline at end of file