Skip to content

Commit e05e229

Browse files
committed
[added] Transition#cancel
[changed] Transition#to/from => Transition.to/from
1 parent b06e34f commit e05e229

File tree

2 files changed

+53
-60
lines changed

2 files changed

+53
-60
lines changed

modules/Transition.js

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* jshint -W058 */
2-
var assign = require('react/lib/Object.assign');
2+
3+
var Cancellation = require('./Cancellation');
34
var Redirect = require('./Redirect');
45

56
/**
@@ -14,69 +15,61 @@ function Transition(path, retry) {
1415
this.retry = retry.bind(this);
1516
}
1617

17-
assign(Transition.prototype, {
18-
19-
abort: function (reason) {
20-
if (this.abortReason == null)
21-
this.abortReason = reason || 'ABORT';
22-
},
18+
Transition.prototype.abort = function (reason) {
19+
if (this.abortReason == null)
20+
this.abortReason = reason || 'ABORT';
21+
};
2322

24-
redirect: function (to, params, query) {
25-
this.abort(new Redirect(to, params, query));
26-
},
23+
Transition.prototype.redirect = function (to, params, query) {
24+
this.abort(new Redirect(to, params, query));
25+
};
2726

28-
from: function (routes, components, callback) {
29-
var self = this;
27+
Transition.prototype.cancel = function () {
28+
this.abort(new Cancellation);
29+
};
3030

31-
var runHooks = routes.reduce(function (callback, route, index) {
32-
return function (error) {
33-
if (error || self.abortReason) {
34-
callback(error);
35-
} else if (route.willTransitionFrom) {
36-
try {
37-
route.willTransitionFrom(self, components[index], callback);
31+
Transition.from = function (transition, routes, components, callback) {
32+
routes.reduce(function (callback, route, index) {
33+
return function (error) {
34+
if (error || transition.abortReason) {
35+
callback(error);
36+
} else if (route.willTransitionFrom) {
37+
try {
38+
route.willTransitionFrom(transition, components[index], callback);
3839

39-
// If there is no callback in the argument list, call it automatically.
40-
if (route.willTransitionFrom.length < 3)
41-
callback();
42-
} catch (e) {
43-
callback(e);
44-
}
45-
} else {
46-
callback();
40+
// If there is no callback in the argument list, call it automatically.
41+
if (route.willTransitionFrom.length < 3)
42+
callback();
43+
} catch (e) {
44+
callback(e);
4745
}
48-
};
49-
}, callback);
50-
51-
runHooks();
52-
},
46+
} else {
47+
callback();
48+
}
49+
};
50+
}, callback)();
51+
};
5352

54-
to: function (routes, params, query, callback) {
55-
var self = this;
53+
Transition.to = function (transition, routes, params, query, callback) {
54+
routes.reduceRight(function (callback, route) {
55+
return function (error) {
56+
if (error || transition.abortReason) {
57+
callback(error);
58+
} else if (route.willTransitionTo) {
59+
try {
60+
route.willTransitionTo(transition, params, query, callback);
5661

57-
var runHooks = routes.reduceRight(function (callback, route) {
58-
return function (error) {
59-
if (error || self.abortReason) {
60-
callback(error);
61-
} else if (route.willTransitionTo) {
62-
try {
63-
route.willTransitionTo(self, params, query, callback);
64-
65-
// If there is no callback in the argument list, call it automatically.
66-
if (route.willTransitionTo.length < 4)
67-
callback();
68-
} catch (e) {
69-
callback(e);
70-
}
71-
} else {
72-
callback();
62+
// If there is no callback in the argument list, call it automatically.
63+
if (route.willTransitionTo.length < 4)
64+
callback();
65+
} catch (e) {
66+
callback(e);
7367
}
74-
};
75-
}, callback);
76-
77-
runHooks();
78-
}
79-
80-
});
68+
} else {
69+
callback();
70+
}
71+
};
72+
}, callback)();
73+
};
8174

8275
module.exports = Transition;

modules/createRouter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function createRouter(options) {
162162

163163
cancelPendingTransition: function () {
164164
if (pendingTransition) {
165-
pendingTransition.abort(new Cancellation);
165+
pendingTransition.cancel();
166166
pendingTransition = null;
167167
}
168168
},
@@ -385,11 +385,11 @@ function createRouter(options) {
385385

386386
var fromComponents = mountedComponents.slice(prevRoutes.length - fromRoutes.length);
387387

388-
transition.from(fromRoutes, fromComponents, function (error) {
388+
Transition.from(transition, fromRoutes, fromComponents, function (error) {
389389
if (error || transition.abortReason)
390390
return dispatchHandler.call(Router, error, transition); // No need to continue.
391391

392-
transition.to(toRoutes, nextParams, nextQuery, function (error) {
392+
Transition.to(transition, toRoutes, nextParams, nextQuery, function (error) {
393393
dispatchHandler.call(Router, error, transition, {
394394
path: path,
395395
action: action,

0 commit comments

Comments
 (0)