Skip to content

feat(uiSref): add support for transition options #789

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
16 changes: 11 additions & 5 deletions src/stateDirectives.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function parseStateRef(ref) {
var parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/);
if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'");
return { state: parsed[1], paramExpr: parsed[3] || null };
var parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\(({[^}]*})[,\s]*({[^}]*})?\))?$/);
if (!parsed || parsed.length !== 5) throw new Error("Invalid state ref '" + ref + "'");
return { state: parsed[1], paramExpr: parsed[3] || null, optionExpr: parsed[4] || null };
}

function stateContext(el) {
Expand Down Expand Up @@ -55,7 +55,7 @@ function $StateRefDirective($state, $timeout) {
require: '?^uiSrefActive',
link: function(scope, element, attrs, uiSrefActive) {
var ref = parseStateRef(attrs.uiSref);
var params = null, url = null, base = stateContext(element) || $state.$current;
var params = null, url = null, base = stateContext(element) || $state.$current, options = null;
var isForm = element[0].nodeName === "FORM";
var attr = isForm ? "action" : "href", nav = true;

Expand All @@ -81,6 +81,12 @@ function $StateRefDirective($state, $timeout) {
}, true);
params = scope.$eval(ref.paramExpr);
}
if (ref.optionExpr) {
scope.$watch(ref.optionExpr, function(newVal, oldVal) {
if (newVal !== options) options = newVal;
});
options = scope.$eval(ref.optionExpr);
}
update();

if (isForm) return;
Expand All @@ -90,7 +96,7 @@ 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:
$timeout(function() {
$state.go(ref.state, params, { relative: base });
$state.go(ref.state, params, angular.extend({ relative: base }, options));
});
e.preventDefault();
}
Expand Down
27 changes: 27 additions & 0 deletions test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,33 @@ describe('uiStateRef', function() {
expect($state.$current.name).toBe("contacts");
}));
});

describe('transition options', function() {

beforeEach(inject(function($rootScope, $compile, $state) {
el = angular.element('<a ui-sref="contacts.item.detail({ id: contact.id }, { reload: true })">Details</a>');
scope = $rootScope;
scope.contact = { id: 5 };
scope.$apply();

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

it('uses transition options', inject(function($q, $timeout, $state) {
var transitionOptions;

spyOn($state, 'go').andCallFake(function(state, params, options) {
transitionOptions = options;
});

triggerClick(el);
$timeout.flush();

expect(transitionOptions.reload).toEqual(true);
}));

});
});

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