Skip to content

feat(uiSref): add ui-sref-on attribute #1986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/stateDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ function stateContext(el) {
* using the `ui-sref-opts` attribute. Options are restricted to `location`, `inherit`,
* and `reload`.
*
* You can invoke an expression when this state transition is initiated using
* the `ui-sref-on` attribute. The promise returned by {@link
* ui.router.state.$state#methods_go $state.go()} is injected into the expression as
* `$template`.
*
* @example
* Here's an example of how you'd use ui-sref and how it would compile. If you have the
* following template:
Expand Down Expand Up @@ -75,6 +80,7 @@ function stateContext(el) {
*
* @param {string} ui-sref 'stateName' can be any valid absolute or relative state
* @param {Object} ui-sref-opts options to pass to {@link ui.router.state.$state#go $state.go()}
* @param {expression} ui-sref-on expression to evaluate when transition starts. Result of {@link ui.router.state.$state#methods_go $state.go()} is injected as '$transition'
*/
$StateRefDirective.$inject = ['$state', '$timeout'];
function $StateRefDirective($state, $timeout) {
Expand Down Expand Up @@ -134,7 +140,13 @@ 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) {
if (angular.version.minor < 2) throw new Error("uiSrefOn not supported by angular < 1.2.0");
scope.$eval(attrs.uiSrefOn, {
$transition: $transition
});
}
});
e.preventDefault();

Expand Down
36 changes: 36 additions & 0 deletions test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,42 @@ describe('uiStateRef', function() {
expect(transitionOptions.notify).toBeUndefined();
}));
});

describe('transition expression', function() {
beforeEach(inject(function($rootScope, $compile) {
el = angular.element('<a ui-sref="contacts.item.detail({id: 5})" ui-sref-on="loading = $transition">Details</a>');
scope = $rootScope;
scope.loading = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@warrenseymour I thought of a way to fix the tests for the 1.0.x thing until we drop support for it:

var _transition = null;
el = angular.element('<a ui-sref="contacts.item.detail({id: 5})" ui-sref-on="load($transition)">Details</a>');
scope.load = function(transition) {
  _transition = transition;
}

Then s/scope.loading/_transition/.


$compile(el)(scope);
scope.$digest();
}));

it('applies an expression when a transition begins', inject(function($rootScope, $timeout, $state) {
if (angular.version.minor < 2) return;

var newState;

expect(scope.loading).toBe(null);
triggerClick(el);
$timeout.flush();

expect(scope.loading.then).toEqual(jasmine.any(Function));

scope.loading.then(function(_newState) {
newState = _newState;
});

$rootScope.$digest();
expect(newState).toEqual($state.get('contacts.item.detail'));
}));

it('throws on < 1.2.0 due to $parse promise mangling', inject(function($timeout) {
if (angular.version.minor >= 2) return;
triggerClick(el);
expect($timeout.flush).toThrow("uiSrefOn not supported by angular < 1.2.0");
}));
});
});

describe('uiSrefActive', function() {
Expand Down