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

Don’t share view registry across containers #10599

Merged
merged 3 commits into from
Mar 10, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ var Application = Namespace.extend(DeferredMixin, {
buildDefaultInstance: function() {
var instance = this.buildInstance();

// For the default instance only, set the view registry to the global
// Ember.View.views hash for backwards-compatibility.
var registry = instance.applicationRegistry;
Copy link
Member

Choose a reason for hiding this comment

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

wouldn't it be simpler to do
EmberView.views = instance.container.lookup('-view-registry:main')

registry.unregister('-view-registry:main');
registry.register('-view-registry:main', EmberView.views);
registry.optionsForType('-view-registry', { instantiate: false });

// TODO2.0: Legacy support for App.__container__
// and global methods on App that rely on a single,
// default instance.
Expand Down Expand Up @@ -1006,6 +1013,10 @@ Application.reopenClass({
registry.register('view:select', SelectView);
registry.register('view:-outlet', OutletView);

registry.register('-view-registry:main', { create: function() { return {}; } });

registry.injection('view', '_viewRegistry', '-view-registry:main');

registry.register('view:default', _MetamorphView);
registry.register('view:toplevel', EmberView.extend());

Expand Down
42 changes: 42 additions & 0 deletions packages/ember-application/tests/system/visit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import run from "ember-metal/run_loop";
import Application from "ember-application/system/application";
import ApplicationInstance from "ember-application/system/application-instance";
import Router from "ember-routing/system/router";
import View from "ember-views/views/view";
import compile from "ember-template-compiler/system/compile";

function createApplication() {
Expand Down Expand Up @@ -82,4 +83,45 @@ if (Ember.FEATURES.isEnabled('ember-application-visit')) {
assert.ok(false, "The visit() promise was rejected: " + error);
});
});

QUnit.test("Views created via visit() are not added to the global views hash", function(assert) {
QUnit.expect(6);
QUnit.stop();

var app;

run(function() {
app = createApplication();
app.instanceInitializer({
name: 'register-application-template',
initialize: function(app) {
app.registry.register('template:application', compile('<h1>Hello world</h1> {{view "child"}}'));
app.registry.register('view:application', View.extend({
elementId: 'my-cool-app'
}));
app.registry.register('view:child', View.extend({
elementId: 'child-view'
}));
}
});
});

assert.equal(Ember.$('#qunit-fixture').children().length, 0, "there are no elements in the fixture element");

app.visit('/').then(function(instance) {
QUnit.start();
assert.ok(instance instanceof ApplicationInstance, "promise is resolved with an ApplicationInstance");

run(instance.view, 'appendTo', '#qunit-fixture');
assert.equal(Ember.$("#qunit-fixture > #my-cool-app h1").text(), "Hello world", "the application was rendered once the promise resolves");
assert.strictEqual(View.views['my-cool-app'], undefined, "view was not registered globally");
ok(instance.container.lookup('-view-registry:main')['my-cool-app'] instanceof View, "view was registered on the instance's view registry");
ok(instance.container.lookup('-view-registry:main')['child-view'] instanceof View, "child view was registered on the instance's view registry");

instance.destroy();
}, function(error) {
QUnit.start();
assert.ok(false, "The visit() promise was rejected: " + error);
});
});
}
6 changes: 3 additions & 3 deletions packages/ember-views/lib/views/states/in_dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ merge(inDOM, {
// Register the view for event handling. This hash is used by
// Ember.EventDispatcher to dispatch incoming events.
if (!view.isVirtual) {
Ember.assert("Attempted to register a view with an id already in use: "+view.elementId, !View.views[view.elementId]);
View.views[view.elementId] = view;
Ember.assert("Attempted to register a view with an id already in use: "+view.elementId, !view._viewRegistry[view.elementId]);
Copy link
Member

Choose a reason for hiding this comment

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

Would probably want to move these asserts to the _register function

view._register();
}

Ember.runInDebug(function() {
Expand All @@ -36,7 +36,7 @@ merge(inDOM, {
if (!View) { View = requireModule('ember-views/views/view')["default"]; } // ES6TODO: this sucks. Have to avoid cycles...
Copy link
Member

Choose a reason for hiding this comment

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

Seems like we don't need this line anymore


if (!this.isVirtual) {
delete View.views[view.elementId];
view._unregister();
}
},

Expand Down
30 changes: 30 additions & 0 deletions packages/ember-views/lib/views/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,10 @@ var View = CoreView.extend(
}

this._super.apply(this, arguments);

if (!this._viewRegistry) {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this handled by the buildDefaultInstance? Why do we need this?

Copy link
Member

Choose a reason for hiding this comment

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

@igorT - Not all view creation goes through the container via a fully booted application (most specifically those created in tests). It is possible that we might not care if those end up in Ember.View.views though...

Copy link
Member

Choose a reason for hiding this comment

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

Those tests should either then not require global Ember.View.views or need to mock it/require it. We can leave that for a later refactor though

Copy link
Member

Choose a reason for hiding this comment

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

@igorT - Yep, I agree.

Copy link
Member Author

Choose a reason for hiding this comment

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

@rwjblue is correct, there are a ton of tests (and presumably apps) that create container-less Views. That's what this code is handling.

this._viewRegistry = View.views;
}
},

__defineNonEnumerable: function(property) {
Expand Down Expand Up @@ -1371,6 +1375,32 @@ var View = CoreView.extend(
return this.currentState.handleEvent(this, eventName, evt);
},

/**
Registers the view in the view registry, keyed on the view's `elementId`.
This is used by the EventDispatcher to locate the view in response to
events.

This method should only be called once the view has been inserted into the
DOM.

@method _register
@private
*/
_register: function() {
this._viewRegistry[this.elementId] = this;
},

/**
Removes the view from the view registry. This should be called when the
view is removed from DOM.

@method _unregister
@private
*/
_unregister: function() {
delete this._viewRegistry[this.elementId];
},

registerObserver: function(root, path, target, observer) {
if (!observer && 'function' === typeof target) {
observer = target;
Expand Down