Skip to content

Commit c355119

Browse files
author
cristHian Gz
committed
Bind ui-sref to DOM events
1 parent 5fd8177 commit c355119

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

src/stateDirectives.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,18 @@ function $StateRefDirective($state, $timeout) {
145145

146146
if (!type.clickable) return;
147147
hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
148-
element[element.on ? 'on' : 'bind']("click", hookFn);
148+
149+
var htmlEvent = isArray(def.options.event) ? def.options.event : ['click'];
150+
var on = element.on ? 'on' : 'bind';
151+
for (var i = 0; i < htmlEvent.length; i++) {
152+
element[on](htmlEvent[i], hookFn);
153+
}
154+
149155
scope.$on('$destroy', function() {
150-
element[element.off ? 'off' : 'unbind']("click", hookFn);
156+
var off = element.off ? 'off' : 'unbind';
157+
for (var i = 0; i < htmlEvent.length; i++) {
158+
element[off](htmlEvent[i], hookFn);
159+
}
151160
});
152161
}
153162
};

test/stateDirectivesSpec.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('uiStateRef', function() {
141141
ctrlKey: undefined,
142142
shiftKey: undefined,
143143
altKey: undefined,
144-
button: undefined
144+
button: undefined
145145
});
146146
timeoutFlush();
147147
$q.flush();
@@ -156,7 +156,7 @@ describe('uiStateRef', function() {
156156

157157
timeoutFlush();
158158
$q.flush();
159-
159+
160160
expect($state.current.name).toEqual('top');
161161
expect($stateParams).toEqualData({ });
162162
}));
@@ -222,7 +222,7 @@ describe('uiStateRef', function() {
222222

223223
it('should allow passing params to current state', inject(function($compile, $rootScope, $state) {
224224
$state.current.name = 'contacts.item.detail';
225-
225+
226226
el = angular.element("<a ui-sref=\"{id: $index}\">Details</a>");
227227
$rootScope.$index = 3;
228228
$rootScope.$apply();
@@ -231,10 +231,10 @@ describe('uiStateRef', function() {
231231
$rootScope.$digest();
232232
expect(el.attr('href')).toBe('#/contacts/3');
233233
}));
234-
234+
235235
it('should allow multi-line attribute values when passing params to current state', inject(function($compile, $rootScope, $state) {
236236
$state.current.name = 'contacts.item.detail';
237-
237+
238238
el = angular.element("<a ui-sref=\"{\n\tid: $index\n}\">Details</a>");
239239
$rootScope.$index = 3;
240240
$rootScope.$apply();
@@ -460,6 +460,37 @@ describe('uiStateRef', function() {
460460
expect($state.$current.name).toBe("contacts");
461461
}));
462462
});
463+
464+
describe('DOM event bind to ui-sref', function() {
465+
466+
it('should bind click event by default', inject(function($rootScope, $compile, $state, $timeout) {
467+
el = angular.element('<a ui-sref="contacts"></a>');
468+
template = $compile(el)($rootScope);
469+
$rootScope.$digest();
470+
471+
expect($state.current.name).toEqual('top');
472+
473+
triggerClick(el);
474+
$timeout.flush();
475+
476+
expect($state.current.name).toEqual('contacts');
477+
}));
478+
479+
it('should bind events from ui-sref-opts', inject(function($rootScope, $compile, $state, $timeout) {
480+
el = angular.element('<input type="text" ui-sref="contacts" ui-sref-opts="{ event: [\'change\'] }">');
481+
template = $compile(el)($rootScope);
482+
$rootScope.$digest();
483+
484+
expect($state.current.name).toEqual('top');
485+
486+
var change = document.createEvent("HTMLEvents");
487+
change.initEvent("change", false, true);
488+
el[0].dispatchEvent(change);
489+
$timeout.flush();
490+
491+
expect($state.current.name).toEqual('contacts');
492+
}));
493+
});
463494
});
464495

465496
describe('uiSrefActive', function() {

0 commit comments

Comments
 (0)