Skip to content

Commit

Permalink
Merge pull request #12057 from rwjblue/instance-initializers-custom-e…
Browse files Browse the repository at this point in the history
…vents

Allow instanceInitializers to set `customEvents`.
  • Loading branch information
rwjblue committed Aug 12, 2015
2 parents 0c95d49 + f12a9f4 commit c490566
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { computed } from 'ember-metal/computed';
import Registry from 'container/registry';
import RegistryProxy from 'ember-runtime/mixins/registry_proxy';
import ContainerProxy from 'ember-runtime/mixins/container_proxy';
import assign from 'ember-metal/assign';

/**
The `ApplicationInstance` encapsulates all of the stateful aspects of a
Expand Down Expand Up @@ -188,8 +189,10 @@ let ApplicationInstance = EmberObject.extend(RegistryProxy, ContainerProxy, {
setupEventDispatcher() {
var dispatcher = this.lookup('event_dispatcher:main');
var applicationCustomEvents = get(this.application, 'customEvents');
var instanceCustomEvents = get(this, 'customEvents');

dispatcher.setup(applicationCustomEvents, this.rootElement);
var customEvents = assign({}, applicationCustomEvents, instanceCustomEvents);
dispatcher.setup(customEvents, this.rootElement);

return dispatcher;
},
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,12 @@ var Application = Namespace.extend(RegistryProxy, {
*/
didBecomeReady() {
if (this.autoboot) {
this.runInstanceInitializers(this.__deprecatedInstance__);

if (environment.hasDOM) {
this.__deprecatedInstance__.setupEventDispatcher();
}

this.runInstanceInitializers(this.__deprecatedInstance__);
this.ready(); // user hook
this.__deprecatedInstance__.startRouting();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,41 @@ QUnit.test('customEvents added to the application before setupEventDispatcher',

appInstance.setupEventDispatcher();
});

QUnit.test('customEvents added to the application before setupEventDispatcher', function(assert) {
assert.expect(1);

run(function() {
appInstance = ApplicationInstance.create({ application: app });
});

app.customEvents = {
awesome: 'sauce'
};

var eventDispatcher = appInstance.lookup('event_dispatcher:main');
eventDispatcher.setup = function(events) {
assert.equal(events.awesome, 'sauce');
};

appInstance.setupEventDispatcher();
});

QUnit.test('customEvents added to the application instance before setupEventDispatcher', function(assert) {
assert.expect(1);

run(function() {
appInstance = ApplicationInstance.create({ application: app });
});

appInstance.customEvents = {
awesome: 'sauce'
};

var eventDispatcher = appInstance.lookup('event_dispatcher:main');
eventDispatcher.setup = function(events) {
assert.equal(events.awesome, 'sauce');
};

appInstance.setupEventDispatcher();
});
48 changes: 41 additions & 7 deletions packages/ember/tests/application_lifecycle_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import isEnabled from 'ember-metal/features';

var compile = Ember.HTMLBars.compile;

var ApplicationSubclass, App, container, router;
var App, container, router;

function setupApp() {
function setupApp(klass) {
Ember.run(function() {
App = ApplicationSubclass.create({
App = klass.create({
rootElement: '#qunit-fixture'
});

Expand All @@ -24,9 +24,7 @@ function setupApp() {

QUnit.module('Application Lifecycle', {
setup() {
ApplicationSubclass = Ember.Application.extend();

setupApp();
setupApp(Ember.Application.extend());
},

teardown() {
Expand Down Expand Up @@ -129,6 +127,8 @@ QUnit.test('initializers can augment an applications customEvents hash', functio

Ember.run(App, 'destroy');

var ApplicationSubclass = Ember.Application.extend();

if (isEnabled('ember-registry-container-reform')) {
ApplicationSubclass.initializer({
name: 'customize-things',
Expand All @@ -149,7 +149,7 @@ QUnit.test('initializers can augment an applications customEvents hash', functio
});
}

setupApp();
setupApp(ApplicationSubclass);

App.FooBarComponent = Ember.Component.extend({
wowza() {
Expand All @@ -166,3 +166,37 @@ QUnit.test('initializers can augment an applications customEvents hash', functio
Ember.$('#wowza-thingy').trigger('wowza');
});
});

QUnit.test('instanceInitializers can augment an the customEvents hash', function(assert) {
assert.expect(1);

Ember.run(App, 'destroy');

var ApplicationSubclass = Ember.Application.extend();

ApplicationSubclass.instanceInitializer({
name: 'customize-things',
initialize(application) {
application.customEvents = {
herky: 'jerky'
};
}
});

setupApp(ApplicationSubclass);

App.FooBarComponent = Ember.Component.extend({
jerky() {
assert.ok(true, 'fired the event!');
}
});

Ember.TEMPLATES['application'] = compile(`{{foo-bar}}`);
Ember.TEMPLATES['components/foo-bar'] = compile(`<div id='herky-thingy'></div>`);

Ember.run(App, 'advanceReadiness');

Ember.run(function() {
Ember.$('#herky-thingy').trigger('herky');
});
});

0 comments on commit c490566

Please sign in to comment.