Skip to content
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

[FEATURE ember-router-willtransition] #10274

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e88cc10
Add support for router willTransition hook
asakusuma Jan 23, 2015
db94669
Wrap willTransition in feature flag
asakusuma Jan 23, 2015
cef7bb2
Wrap willTransition test in feature flag
asakusuma Jan 23, 2015
680f413
[BUGFIX release] view keyword should not chain directly off parent's …
mmun Jan 23, 2015
96f2862
Signal move to StandardIssueLabels
wagenet Jan 23, 2015
d1e6506
[BUGFIX beta] Fix broken unregisterWaiter semantics
ef4 Jan 23, 2015
e102a26
[BUGFIX beta] Allow unbound helpers to add attributes.
rwjblue Dec 22, 2014
8f87799
[BUGFIX beta] Ensure compat helpers calling options.fn work.
rwjblue Jan 24, 2015
66f1e9b
Moves route-recognizer to a NPM dep, bumps emberjs-build
danmcclain Jan 25, 2015
ff1bac1
Make computed macros named exports.
rwjblue Jan 25, 2015
216cb87
Fix accidental overriding of imports.
rwjblue Jan 26, 2015
235ff47
Export functions instead of assigning variables for export.
rwjblue Jan 26, 2015
4992c78
Do not use variable with same name as export function.
rwjblue Jan 26, 2015
436100e
Import computed macros directly, don't rely on import mutation.
rwjblue Jan 26, 2015
edab56c
[DOCS]Mark some Em.CoreObject's members as static
e00dan Jan 25, 2015
42dc206
Remove trailing spaces
e00dan Jan 26, 2015
5cf8f44
Merge remote-tracking branch 'upstream/master'
asakusuma Jan 26, 2015
22b6890
Merge pull request #10284 from fivetanley/stream-leak
fivetanley Jan 26, 2015
c544640
Fix punctuation
asakusuma Jan 26, 2015
083cbc3
Merge remote-tracking branch 'upstream/master'
asakusuma Jan 26, 2015
3d652e9
Merge pull request #10287 from seanpdoyle/check-uncheck-test-helper-fix
rwjblue Jan 26, 2015
29b6691
[FEATURE ember-router-willtransition]
asakusuma Jan 26, 2015
8266a5d
Merge branch 'master' of https://github.com/asakusuma/ember.js
asakusuma Jan 26, 2015
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
15 changes: 15 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,18 @@ for a detailed explanation.
```

Added in [#10160](https://github.com/emberjs/ember.js/pull/10160)

* `ember-router-willtransition`


Adds `willTransition` hook to `Ember.Router`. For example:

```js
Ember.Router.extend({
onBeforeTransition: function(transition) {
//doSomething
}.on('willTransition')
});
```

Added in [#10274](https://github.com/emberjs/ember.js/pull/10274)
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"devDependencies": {
"backburner": "https://github.com/ebryn/backburner.js.git#f4bd6a2df221240ed36d140f0c53c036a7ecacad",
"rsvp": "https://github.com/tildeio/rsvp.js.git#3.0.14",
"router.js": "https://github.com/tildeio/router.js.git#a1ffd97dc66a6d9d4e8dd89a72c1c4e21a3328c5",
"router.js": "https://github.com/tildeio/router.js.git#72eb0d336c1c3b7ad3965198b330fe148d935efe",
"dag-map": "https://github.com/krisselden/dag-map.git#e307363256fe918f426e5a646cb5f5062d3245be",
"ember-dev": "https://github.com/emberjs/ember-dev.git#1c30a1666273ab2a9b134a42bad28c774f9ecdfc"
}
Expand Down
3 changes: 2 additions & 1 deletion features.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"ember-metal-stream": null,
"ember-htmlbars-each-with-index": true,
"ember-application-instance-initializers": null,
"ember-application-initializer-context": null
"ember-application-initializer-context": null,
"ember-router-willtransition": true
},
"debugStatements": [
"Ember.warn",
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-htmlbars/lib/helpers/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import Ember from "ember-metal/core"; // Ember.warn, Ember.assert
import { read } from "ember-metal/streams/utils";
import { readViewFactory } from "ember-views/streams/utils";
import View from "ember-views/views/view";
import mergeViewBindings from "ember-htmlbars/system/merge-view-bindings";
Expand Down Expand Up @@ -190,7 +191,7 @@ export function viewHelper(params, hash, options, env) {
params.length <= 2
);

var container = this.container || this._keywords.view.value().container;
var container = this.container || read(this._keywords.view).container;
var viewClassOrInstance;
if (params.length === 0) {
if (container) {
Expand Down
24 changes: 24 additions & 0 deletions packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ var EmberRouter = EmberObject.extend(Evented, {
}
},

/**
Handles notifying any listeners of an impending URL
change.

Triggers the router level `willTransition` hook.

@method willTransition
@private
@since 1.11.0
*/
willTransition: function(oldInfos, newInfos, transition) {
run.once(this, this.trigger, 'willTransition', transition);

if (get(this, 'namespace').LOG_TRANSITIONS) {
Ember.Logger.log("Preparing to transition from '" + EmberRouter._routePath(oldInfos) + "' to '" + EmberRouter._routePath(newInfos) + "'");
}
},

handleURL: function(url) {
// Until we have an ember-idiomatic way of accessing #hashes, we need to
// remove it because router.js doesn't know how to handle it.
Expand Down Expand Up @@ -419,6 +437,12 @@ var EmberRouter = EmberObject.extend(Evented, {
router.didTransition = function(infos) {
emberRouter.didTransition(infos);
};

if (Ember.FEATURES.isEnabled('ember-router-willtransition')) {
router.willTransition = function(oldInfos, newInfos, transition) {
emberRouter.willTransition(oldInfos, newInfos, transition);
};
}
},

_serializeQueryParams: function(targetRouteName, queryParams) {
Expand Down
5 changes: 2 additions & 3 deletions packages/ember-views/lib/views/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ var Component = View.extend(TargetActionSupport, ComponentTemplateDeprecation, {

init: function() {
this._super();
this._keywords.view = this;
set(this, 'context', this);
set(this, 'controller', this);
},
Expand Down Expand Up @@ -158,9 +159,7 @@ var Component = View.extend(TargetActionSupport, ComponentTemplateDeprecation, {
*/
templateName: null,

_setupKeywords: function() {
this._keywords.view.setSource(this);
},
_setupKeywords: function() {},

_yield: function(context, options, morph, blockArguments) {
var view = options.data.view;
Expand Down
7 changes: 3 additions & 4 deletions packages/ember-views/lib/views/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
observer,
beforeObserver
} from "ember-metal/mixin";
import SimpleStream from "ember-metal/streams/simple";
import KeyStream from "ember-views/streams/key_stream";
import StreamBinding from "ember-metal/streams/stream_binding";
import ContextStream from "ember-views/streams/context_stream";
Expand Down Expand Up @@ -1070,7 +1069,7 @@ var View = CoreView.extend({
if (contextView) {
var parentKeywords = contextView._keywords;

keywords.view.setSource(this.isVirtual ? parentKeywords.view : this);
keywords.view = this.isVirtual ? parentKeywords.view : this;

for (var name in parentKeywords) {
if (keywords[name]) {
Expand All @@ -1080,7 +1079,7 @@ var View = CoreView.extend({
keywords[name] = parentKeywords[name];
}
} else {
keywords.view.setSource(this.isVirtual ? null : this);
keywords.view = this.isVirtual ? null : this;
}
},

Expand Down Expand Up @@ -1753,8 +1752,8 @@ var View = CoreView.extend({
if (!this._keywords) {
this._keywords = create(null);
}
this._keywords.view = new SimpleStream();
this._keywords._view = this;
this._keywords.view = undefined;
this._keywords.controller = new KeyStream(this, 'controller');
this._setupKeywords();

Expand Down
21 changes: 11 additions & 10 deletions packages/ember-views/tests/views/container_view_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { get } from "ember-metal/property_get";
import { set } from "ember-metal/property_set";
import run from "ember-metal/run_loop";
import { computed } from "ember-metal/computed";
import { read } from "ember-metal/streams/utils";
import Controller from "ember-runtime/controllers/controller";
import jQuery from "ember-views/system/jquery";
import View from "ember-views/views/view";
Expand Down Expand Up @@ -300,8 +301,8 @@ test("if a ContainerView starts with a currentView, it is rendered as a child vi
equal(container.objectAt(0), mainView, "should have the currentView as the only child view");
equal(mainView.get('parentView'), container, "parentView is setup");
equal(context, container.get('context'), 'context preserved');
equal(mainView._keywords.controller.value(), controller, 'controller keyword is setup');
equal(mainView._keywords.view.value(), mainView, 'view keyword is setup');
equal(read(mainView._keywords.controller), controller, 'controller keyword is setup');
equal(read(mainView._keywords.view), mainView, 'view keyword is setup');
});

test("if a ContainerView is created with a currentView, it is rendered as a child view", function() {
Expand Down Expand Up @@ -329,8 +330,8 @@ test("if a ContainerView is created with a currentView, it is rendered as a chil
equal(container.objectAt(0), mainView, "should have the currentView as the only child view");
equal(mainView.get('parentView'), container, "parentView is setup");
equal(context, container.get('context'), 'context preserved');
equal(mainView._keywords.controller.value(), controller, 'controller keyword is setup');
equal(mainView._keywords.view.value(), mainView, 'view keyword is setup');
equal(read(mainView._keywords.controller), controller, 'controller keyword is setup');
equal(read(mainView._keywords.view), mainView, 'view keyword is setup');
});

test("if a ContainerView starts with no currentView and then one is set, the ContainerView is updated", function() {
Expand Down Expand Up @@ -364,8 +365,8 @@ test("if a ContainerView starts with no currentView and then one is set, the Con
equal(container.objectAt(0), mainView, "should have the currentView as the only child view");
equal(mainView.get('parentView'), container, "parentView is setup");
equal(context, container.get('context'), 'context preserved');
equal(mainView._keywords.controller.value(), controller, 'controller keyword is setup');
equal(mainView._keywords.view.value(), mainView, 'view keyword is setup');
equal(read(mainView._keywords.controller), controller, 'controller keyword is setup');
equal(read(mainView._keywords.view), mainView, 'view keyword is setup');
});

test("if a ContainerView starts with a currentView and then is set to null, the ContainerView is updated", function() {
Expand Down Expand Up @@ -394,8 +395,8 @@ test("if a ContainerView starts with a currentView and then is set to null, the
equal(container.objectAt(0), mainView, "should have the currentView as the only child view");
equal(mainView.get('parentView'), container, "parentView is setup");
equal(context, container.get('context'), 'context preserved');
equal(mainView._keywords.controller.value(), controller, 'controller keyword is setup');
equal(mainView._keywords.view.value(), mainView, 'view keyword is setup');
equal(read(mainView._keywords.controller), controller, 'controller keyword is setup');
equal(read(mainView._keywords.view), mainView, 'view keyword is setup');

run(function() {
set(container, 'currentView', null);
Expand Down Expand Up @@ -431,8 +432,8 @@ test("if a ContainerView starts with a currentView and then is set to null, the
equal(container.objectAt(0), mainView, "should have the currentView as the only child view");
equal(mainView.get('parentView'), container, "parentView is setup");
equal(context, container.get('context'), 'context preserved');
equal(mainView._keywords.controller.value(), controller, 'controller keyword is setup');
equal(mainView._keywords.view.value(), mainView, 'view keyword is setup');
equal(read(mainView._keywords.controller), controller, 'controller keyword is setup');
equal(read(mainView._keywords.view), mainView, 'view keyword is setup');

run(function() {
set(container, 'currentView', null);
Expand Down
48 changes: 48 additions & 0 deletions packages/ember/tests/routing/basic_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2738,6 +2738,54 @@ test("Route silently fails when cleaning an outlet from an inactive view", funct
Ember.run(function() { router.send('hideModal'); });
});

if (Ember.FEATURES.isEnabled('ember-router-willtransition')) {
test("Router `willTransition` hook passes in cancellable transition", function() {
// Should hit willTransition 3 times, once for the initial route, and then 2 more times
// for the two handleURL calls below
expect(3);

Router.map(function() {
this.route("nork");
this.route("about");
});

Router.reopen({
init: function() {
this._super();
this.on('willTransition', this.testWillTransitionHook);
},
testWillTransitionHook: function(transition, url) {
ok(true, "willTransition was called " + url);
transition.abort();
}
});

App.LoadingRoute = Ember.Route.extend({
activate: function() {
ok(false, "LoadingRoute was not entered");
}
});

App.NorkRoute = Ember.Route.extend({
activate: function() {
ok(false, "NorkRoute was not entered");
}
});

App.AboutRoute = Ember.Route.extend({
activate: function() {
ok(false, "AboutRoute was not entered");
}
});

bootApplication();

// Attempted transitions out of index should abort.
Ember.run(router, 'handleURL', '/nork');
Ember.run(router, 'handleURL', '/about');
});
}

test("Aborting/redirecting the transition in `willTransition` prevents LoadingRoute from being entered", function() {
expect(8);

Expand Down