Skip to content

Commit

Permalink
Merge pull request #12947 from btecu/insertAt
Browse files Browse the repository at this point in the history
[BUGFIX beta] Don't use prototype extensions ({add,remove}ArrayObserver)
  • Loading branch information
stefanpenner committed Feb 19, 2016
2 parents d60236d + c77eb92 commit 0b80c18
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 21 deletions.
14 changes: 9 additions & 5 deletions packages/ember-extension-support/lib/data_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import EmberObject from 'ember-runtime/system/object';
import { A as emberA } from 'ember-runtime/system/native_array';
import Application from 'ember-application/system/application';
import { getOwner } from 'container/owner';
import { objectAt } from 'ember-runtime/mixins/array';
import {
addArrayObserver,
removeArrayObserver,
objectAt
} from 'ember-runtime/mixins/array';

/**
@module ember
Expand Down Expand Up @@ -219,11 +223,11 @@ export default EmberObject.extend({
};

var observer = { didChange: contentDidChange, willChange() { return this; } };
records.addArrayObserver(this, observer);
addArrayObserver(records, this, observer);

release = () => {
releaseMethods.forEach(function(fn) { fn(); });
records.removeArrayObserver(this, observer);
removeArrayObserver(records, this, observer);
this.releaseMethods.removeObject(release);
};

Expand Down Expand Up @@ -298,10 +302,10 @@ export default EmberObject.extend({
willChange() { return this; }
};

records.addArrayObserver(this, observer);
addArrayObserver(records, this, observer);

var release = () => {
records.removeArrayObserver(this, observer);
removeArrayObserver(records, this, observer);
};

return release;
Expand Down
12 changes: 10 additions & 2 deletions packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ function arrayObserversHelper(obj, target, opts, operation, notify) {
return obj;
}

export function addArrayObserver(array, target, opts) {
return arrayObserversHelper(array, target, opts, addListener, false);
}

export function removeArrayObserver(array, target, opts) {
return arrayObserversHelper(array, target, opts, removeListener, true);
}

export function objectAt(content, idx) {
if (content.objectAt) {
return content.objectAt(idx);
Expand Down Expand Up @@ -364,7 +372,7 @@ export default Mixin.create(Enumerable, {
*/

addArrayObserver(target, opts) {
return arrayObserversHelper(this, target, opts, addListener, false);
return addArrayObserver(this, target, opts);
},

/**
Expand All @@ -380,7 +388,7 @@ export default Mixin.create(Enumerable, {
@public
*/
removeArrayObserver(target, opts) {
return arrayObserversHelper(this, target, opts, removeListener, true);
return removeArrayObserver(this, target, opts);
},

/**
Expand Down
14 changes: 9 additions & 5 deletions packages/ember-runtime/lib/system/array_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import EmberObject from 'ember-runtime/system/object';
import MutableArray from 'ember-runtime/mixins/mutable_array';
import Enumerable from 'ember-runtime/mixins/enumerable';
import alias from 'ember-metal/alias';
import { objectAt } from 'ember-runtime/mixins/array';
import {
addArrayObserver,
removeArrayObserver,
objectAt
} from 'ember-runtime/mixins/array';

/**
@module ember
Expand Down Expand Up @@ -140,7 +144,7 @@ var ArrayProxy = EmberObject.extend(MutableArray, {
var content = get(this, 'content');

if (content) {
content.removeArrayObserver(this, {
removeArrayObserver(content, this, {
willChange: 'contentArrayWillChange',
didChange: 'contentArrayDidChange'
});
Expand Down Expand Up @@ -193,7 +197,7 @@ var ArrayProxy = EmberObject.extend(MutableArray, {
if (content) {
assert(`ArrayProxy expects an Array or Ember.ArrayProxy, but you passed ${typeof content}`, isArray(content) || content.isDestroyed);

content.addArrayObserver(this, {
addArrayObserver(content, this, {
willChange: 'contentArrayWillChange',
didChange: 'contentArrayDidChange'
});
Expand Down Expand Up @@ -229,7 +233,7 @@ var ArrayProxy = EmberObject.extend(MutableArray, {
assert(`ArrayProxy expects an Array or Ember.ArrayProxy, but you passed ${typeof arrangedContent}`,
isArray(arrangedContent) || arrangedContent.isDestroyed);

arrangedContent.addArrayObserver(this, {
addArrayObserver(arrangedContent, this, {
willChange: 'arrangedContentArrayWillChange',
didChange: 'arrangedContentArrayDidChange'
});
Expand All @@ -240,7 +244,7 @@ var ArrayProxy = EmberObject.extend(MutableArray, {
var arrangedContent = get(this, 'arrangedContent');

if (arrangedContent) {
arrangedContent.removeArrayObserver(this, {
removeArrayObserver(arrangedContent, this, {
willChange: 'arrangedContentArrayWillChange',
didChange: 'arrangedContentArrayDidChange'
});
Expand Down
10 changes: 7 additions & 3 deletions packages/ember-runtime/tests/mixins/array_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { computed } from 'ember-metal/computed';
import { testBoth } from 'ember-metal/tests/props_helper';
import { ArrayTests } from 'ember-runtime/tests/suites/array';
import EmberObject from 'ember-runtime/system/object';
import EmberArray, { objectAt } from 'ember-runtime/mixins/array';
import EmberArray, {
addArrayObserver,
removeArrayObserver,
objectAt
} from 'ember-runtime/mixins/array';
import { A as emberA } from 'ember-runtime/system/native_array';

/*
Expand Down Expand Up @@ -199,7 +203,7 @@ QUnit.module('notify array observers', {
_after: null
});

obj.addArrayObserver(observer);
addArrayObserver(obj, observer);
},

teardown() {
Expand Down Expand Up @@ -233,7 +237,7 @@ QUnit.test('should notify when called with diff length items', function() {
});

QUnit.test('removing enumerable observer should disable', function() {
obj.removeArrayObserver(observer);
removeArrayObserver(obj, observer);
obj.arrayContentWillChange();
deepEqual(observer._before, null);

Expand Down
8 changes: 6 additions & 2 deletions packages/ember-runtime/tests/suites/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import {
import indexOfTests from 'ember-runtime/tests/suites/array/indexOf';
import lastIndexOfTests from 'ember-runtime/tests/suites/array/lastIndexOf';
import objectAtTests from 'ember-runtime/tests/suites/array/objectAt';
import {
addArrayObserver,
removeArrayObserver
} from 'ember-runtime/mixins/array';

var ObserverClass = EnumerableTestsObserverClass.extend({

observeArray(obj) {
obj.addArrayObserver(this);
addArrayObserver(obj, this);
return this;
},

stopObserveArray(obj) {
obj.removeArrayObserver(this);
removeArrayObserver(obj, this);
return this;
},

Expand Down
12 changes: 8 additions & 4 deletions packages/ember-views/lib/views/collection_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import Ember from 'ember-metal/core';
import { assert, deprecate } from 'ember-metal/debug';
import ContainerView from 'ember-views/views/container_view';
import View from 'ember-views/views/view';
import EmberArray, { objectAt } from 'ember-runtime/mixins/array';
import EmberArray, {
addArrayObserver,
removeArrayObserver,
objectAt
} from 'ember-runtime/mixins/array';
import { get } from 'ember-metal/property_get';
import { set } from 'ember-metal/property_set';
import { computed } from 'ember-metal/computed';
Expand Down Expand Up @@ -224,7 +228,7 @@ var CollectionView = ContainerView.extend(EmptyViewSupport, {
*/
_contentDidChange: observer('content', function() {
var prevContent = this._prevContent;
if (prevContent) { prevContent.removeArrayObserver(this); }
if (prevContent) { removeArrayObserver(prevContent, this); }
var len = prevContent ? get(prevContent, 'length') : 0;
this.arrayWillChange(prevContent, 0, len);

Expand All @@ -233,7 +237,7 @@ var CollectionView = ContainerView.extend(EmptyViewSupport, {
if (content) {
this._prevContent = content;
this._assertArrayLike(content);
content.addArrayObserver(this);
addArrayObserver(content, this);
}

len = content ? get(content, 'length') : 0;
Expand All @@ -260,7 +264,7 @@ var CollectionView = ContainerView.extend(EmptyViewSupport, {
if (!this._super(...arguments)) { return; }

var content = get(this, 'content');
if (content) { content.removeArrayObserver(this); }
if (content) { removeArrayObserver(content, this); }

if (this._createdEmptyView) {
this._createdEmptyView.destroy();
Expand Down

0 comments on commit 0b80c18

Please sign in to comment.