Skip to content

Commit

Permalink
Merge pull request #11685 from stefanpenner/component-cleanup
Browse files Browse the repository at this point in the history
make linkChild idempotent
  • Loading branch information
rwjblue committed Jul 11, 2015
2 parents 98522dd + b0784c1 commit fb49fa0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ before_install:
- "npm config set spin false"
- "npm install -g npm@^2"
- "npm --version"
- "phantomjs --version"

install:
- "npm install"
Expand Down
13 changes: 8 additions & 5 deletions packages/ember-views/lib/mixins/view_child_views_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import setProperties from 'ember-metal/set_properties';

var EMPTY_ARRAY = [];

var ViewChildViewsSupport = Mixin.create({
export default Mixin.create({
/**
Array of child views. You should never edit this array directly.
Instead, use `appendChild` and `removeFromParent`.
Expand Down Expand Up @@ -90,6 +90,8 @@ var ViewChildViewsSupport = Mixin.create({

var attrs = _attrs || {};
var view;

attrs.parentView = this;
attrs.renderer = this.renderer;
attrs._viewRegistry = this._viewRegistry;

Expand Down Expand Up @@ -123,8 +125,11 @@ var ViewChildViewsSupport = Mixin.create({

linkChild(instance) {
instance.container = this.container;
set(instance, 'parentView', this);
instance.trigger('parentViewDidChange');
if (get(instance, 'parentView') !== this) {
// linkChild should be idempotentj
set(instance, 'parentView', this);
instance.trigger('parentViewDidChange');
}
instance.ownerView = this.ownerView;
},

Expand All @@ -133,5 +138,3 @@ var ViewChildViewsSupport = Mixin.create({
instance.trigger('parentViewDidChange');
}
});

export default ViewChildViewsSupport;
25 changes: 24 additions & 1 deletion packages/ember-views/tests/views/view/create_child_view_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { get } from 'ember-metal/property_get';
import run from 'ember-metal/run_loop';
import EmberView from 'ember-views/views/view';
import { on } from 'ember-metal/events';
import { observer } from 'ember-metal/mixin';

var view, myViewClass, newView, container;

Expand Down Expand Up @@ -33,7 +35,28 @@ QUnit.test('should create view from class with any passed attributes', function(
equal(newView.container, container, 'expects to share container with parent');
ok(get(newView, 'isMyView'), 'newView is instance of myView');
equal(get(newView, 'foo'), 'baz', 'view did get custom attributes');
ok(!attrs.parentView, 'the original attributes hash was not mutated');
});

QUnit.test('creating a childView, (via createChildView) should make parentView initial state and not emit change events nore helper actions', function() {
expect(2);

newView = view.createChildView(EmberView.extend({
init() {
this._super(...arguments);
ok(true, 'did init');
},
parentViewDidReallyChange: on('parentViewDidChange', function() {
ok(false, 'expected to NOT emit parentViewDidChange');
}),
controllerDidChange: observer('controller', function() {
ok(false, 'expected to NOT expect controller to change');
}),
parentViewDidChange: observer('parentView', function() {
ok(false, 'expected to NOT expect parentViewto change');
})
}));

equal(newView.get('parentView'), view, 'expected the correct parentView');
});

QUnit.test('should set newView.parentView to receiver', function() {
Expand Down

0 comments on commit fb49fa0

Please sign in to comment.