Skip to content

Commit

Permalink
Merge pull request #43 from swipely/fix_action_halting
Browse files Browse the repository at this point in the history
Correct halt actions when `navigate` is called
  • Loading branch information
barnabyc committed Dec 6, 2013
2 parents 9b8d79a + 0101aa5 commit 62d2b50
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = function(grunt) {
}
});

grunt.registerTask('test', ['jasmine']);
grunt.registerTask('build', ['browserify']);
grunt.registerTask('test', ['build', 'jasmine']);

grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-browserify');
Expand Down
36 changes: 20 additions & 16 deletions aviator.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ window.Aviator = {

/**
@method rewriteRouteTo
@param {String} newRoute
@return {Object}
**/
rewriteRouteTo: function (newRoute) {
var target = {
Expand Down Expand Up @@ -342,7 +344,10 @@ Navigator.prototype = {
request = this.createRequest(uri, queryString, route.matchedRoute);

this._invokeExits(request);
this._invokeActions(route.actions, request, route.options);

// temporary action array that can be halted
this._actions = route.actions;
this._invokeActions(request, route.options);

// collect exits of the current matching route
this._exits = route.exits;
Expand Down Expand Up @@ -417,7 +422,9 @@ Navigator.prototype = {
namedParams = options.namedParams,
queryParams = options.queryParams;

this._haltActionInvocations = true;
// halt any previous action invocations
this._actions = [];
console.log('navigate actions', this._actions.length);

if (queryParams) {
uri += this.serializeQueryParams(queryParams);
Expand Down Expand Up @@ -508,8 +515,8 @@ Navigator.prototype = {
var exit, target, method;

while(this._exits.length) {
exit = this._exits.pop(),
target = exit.target,
exit = this._exits.pop();
target = exit.target;
method = exit.method;

if (!(method in target)) {
Expand All @@ -524,28 +531,25 @@ Navigator.prototype = {
invoke all actions with request and options
@method _invokeActions
@param {Array[Object]} actions
@param {Request} request
@param {Object} options
@protected
**/
_invokeActions: function (actions, request, options) {
var target, method;

this._haltActionInvocations = false;
_invokeActions: function (request, options) {
var action, target, method;

for (var i = 0; i < actions.length; i++ ){
if (this._haltActionInvocations) {
break;
}
console.log('_invokeActions');

target = actions[i].target;
method = actions[i].method;
while (this._actions.length) {
action = this._actions.shift();
target = action.target;
method = action.method;

if (!(method in target)) {
if (!(method in target)) {
throw new Error("Can't call action " + method + ' on target for uri ' + request.uri);
}

console.log('calling method', method);
target[method].call(target, request, options);
}
},
Expand Down
14 changes: 7 additions & 7 deletions spec/navigator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,16 @@ describe('Navigator', function () {

beforeEach(function () {
target = {
actionOne: jasmine.createSpy(),
actionTwo: jasmine.createSpy(),
actionThree: jasmine.createSpy(),
actionFour: jasmine.createSpy()
actionOne: jasmine.createSpy('action one'),
actionTwo: jasmine.createSpy('action two'),
actionThree: jasmine.createSpy('action three'),
actionFour: jasmine.createSpy('action four')
};

request = {};
options = {};

actions = [
subject._actions = [
{ target: target, method: 'actionOne' },
{ target: target, method: 'actionTwo' },
{ target: target, method: 'actionThree' },
Expand All @@ -409,7 +409,7 @@ describe('Navigator', function () {
});

it('invokes all actions with the request and options', function () {
subject._invokeActions(actions, request, options);
subject._invokeActions(request, options);

expect( target.actionOne ).toHaveBeenCalledWith(request, options);
expect( target.actionTwo ).toHaveBeenCalledWith(request, options);
Expand All @@ -427,7 +427,7 @@ describe('Navigator', function () {
});

it('halts all other action invocations', function () {
subject._invokeActions(actions, request, options);
subject._invokeActions(request, options);

expect( target.actionThree ).not.toHaveBeenCalled();
expect( target.actionFour ).not.toHaveBeenCalled();
Expand Down
32 changes: 15 additions & 17 deletions src/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ Navigator.prototype = {
request = this.createRequest(uri, queryString, route.matchedRoute);

this._invokeExits(request);
this._invokeActions(route.actions, request, route.options);

// temporary action array that can be halted
this._actions = route.actions;
this._invokeActions(request, route.options);

// collect exits of the current matching route
this._exits = route.exits;
Expand Down Expand Up @@ -184,7 +187,8 @@ Navigator.prototype = {
namedParams = options.namedParams,
queryParams = options.queryParams;

this._haltActionInvocations = true;
// halt any previous action invocations
this._actions = [];

if (queryParams) {
uri += this.serializeQueryParams(queryParams);
Expand Down Expand Up @@ -275,8 +279,8 @@ Navigator.prototype = {
var exit, target, method;

while(this._exits.length) {
exit = this._exits.pop(),
target = exit.target,
exit = this._exits.pop();
target = exit.target;
method = exit.method;

if (!(method in target)) {
Expand All @@ -291,25 +295,19 @@ Navigator.prototype = {
invoke all actions with request and options
@method _invokeActions
@param {Array[Object]} actions
@param {Request} request
@param {Object} options
@protected
**/
_invokeActions: function (actions, request, options) {
var target, method;

this._haltActionInvocations = false;

for (var i = 0; i < actions.length; i++ ){
if (this._haltActionInvocations) {
break;
}
_invokeActions: function (request, options) {
var action, target, method;

target = actions[i].target;
method = actions[i].method;
while (this._actions.length) {
action = this._actions.shift();
target = action.target;
method = action.method;

if (!(method in target)) {
if (!(method in target)) {
throw new Error("Can't call action " + method + ' on target for uri ' + request.uri);
}

Expand Down

0 comments on commit 62d2b50

Please sign in to comment.