Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
unsubscribe events
Browse files Browse the repository at this point in the history
  • Loading branch information
josex2r committed Nov 7, 2016
1 parent 86382dc commit a82aafd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
28 changes: 25 additions & 3 deletions addon/components/wait-for-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Ember from 'ember';
import layout from '../templates/components/wait-for-render';
import { EVENT_NAME } from '../mixins/wait-for-render';

const { subscribe, unsubscribe } = Ember.Instrumentation;

/**
* Defer component render until route has been rendered.
* Route must implement 'wait-for-render' mixin.
Expand Down Expand Up @@ -63,6 +65,16 @@ export default Ember.Component.extend({
*/
loading: null,

/**
* Stores component instrumentation object.
*
* @property _subscriber
* @type Object
* @default null
* @public
*/
_subscriber: null,

/**
* Subscribe to route events.
*
Expand All @@ -72,10 +84,12 @@ export default Ember.Component.extend({
didInsertElement: function didInsertElement() {
this._super(...arguments);

Ember.subscribe(`${EVENT_NAME}.${this.get('for')}`, {
const subscriber = subscribe(`${EVENT_NAME}.${this.get('for')}`, {
before: Ember.K,
after: () => this._render()
});

this.set('_subscriber', subscriber);
},

/**
Expand All @@ -90,8 +104,16 @@ export default Ember.Component.extend({
}

this.set('_rendered', true);
},

/**
* Destroy instrumentation binding.
*
* @method willDestroyElement
* @private
*/
willDestroyElement() {
unsubscribe(this.get('_subscriber'));
}

}).reopenClass({
positionalParams: ['for']
});
4 changes: 3 additions & 1 deletion addon/mixins/wait-for-render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Ember from 'ember';

const { instrument } = Ember.Instrumentation;

export const EVENT_NAME = 'wait-for-render';

export default Ember.Mixin.create({
Expand All @@ -18,7 +20,7 @@ export default Ember.Mixin.create({
// Defer to the next cycle to allow a browser render
Ember.run.next(this, () => {
// Send a signal when template is fully rendered
Ember.instrument(`${EVENT_NAME}.${this.routeName}`, () => {
instrument(`${EVENT_NAME}.${this.routeName}`, () => {
// Set a flag for debugging purposes
this.set('_rendered', true);
});
Expand Down
7 changes: 5 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "ember-wait-for-render",
"dependencies": {
"ember": "~2.8.0",
"ember": "~1.13.0",
"ember-cli-shims": "0.1.1"
},
"resolutions": {
"ember": "~1.13.0"
}
}
}
27 changes: 22 additions & 5 deletions tests/integration/components/wait-for-render-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { EVENT_NAME } from 'ember-wait-for-render/mixins/wait-for-render';

const { instrument } = Ember.Instrumentation;

moduleForComponent('wait-for-render', 'Integration | Component | wait for render', {
integration: true
});
Expand All @@ -17,17 +19,32 @@ test('it does not renders the content', function(assert) {
assert.equal(this.$().text().trim(), '');
});

test('it renders the content when signal has been triggered', function(assert) {
test('it renders the content when route is ready', function(assert) {
const content = 'foo';

this.set('content', content);
this.render(hbs`{{#wait-for-render}}{{content}}{{/wait-for-render}}`);

Ember.run(() => {
Ember.instrument(`${EVENT_NAME}.foo`, () => {
assert.ok(1);
});
instrument(`${EVENT_NAME}.*`, Ember.K);
});

Ember.run(() => {
assert.equal(this.$().text().trim(), content);
});
});

test('it renders the content when specific route is ready', function(assert) {
const content = 'foo';

this.set('content', content);
this.render(hbs`{{#wait-for-render for=content}}{{content}}{{/wait-for-render}}`);

Ember.run(() => {
instrument(`${EVENT_NAME}.${content}`, Ember.K);
});

assert.equal(this.$().text().trim(), content);
Ember.run(() => {
assert.equal(this.$().text().trim(), content);
});
});
6 changes: 4 additions & 2 deletions tests/unit/mixins/wait-for-render-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import WaitForRenderMixin from 'ember-wait-for-render/mixins/wait-for-render';
import { EVENT_NAME } from 'ember-wait-for-render/mixins/wait-for-render';
import { module, test } from 'qunit';

const { subscribe } = Ember.Instrumentation;

module('Unit | Mixin | wait for render');

test('it works in a Route object', function(assert) {
Expand Down Expand Up @@ -32,7 +34,7 @@ test('it does not works if object is not a Route', function(assert) {
routeName
});

Ember.subscribe(`${EVENT_NAME}.${routeName}`, {
subscribe(`${EVENT_NAME}.${routeName}`, {
before: () => assert.ok(1),
after: () => {
assert.ok(1);
Expand All @@ -53,7 +55,7 @@ test('it toggles \'_rendered\' property', function(assert) {
routeName
});

Ember.subscribe(`${EVENT_NAME}.${routeName}`, {
subscribe(`${EVENT_NAME}.${routeName}`, {
before: () => Ember.K,
after: () => {
assert.ok(subject.get('_rendered'));
Expand Down

0 comments on commit a82aafd

Please sign in to comment.