Skip to content

Commit

Permalink
[BUGFIX beta] Wrap uses of call, apply for _super
Browse files Browse the repository at this point in the history
For CP macros (and other possible cases), `call` or `apply` may be used
to redirect what seems like the "method" of a property. These cases
should be wrapped in case they use `_super`, much as the explicit check
for `_super` usages causes the method to be wrapped.
  • Loading branch information
mixonic committed Sep 18, 2015
1 parent 8c89231 commit 64e40c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/ember-metal/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export function guidFor(obj) {
}
}

const HAS_SUPER_PATTERN = /\.(_super|call\(this|apply\(this)/;

const checkHasSuper = (function () {
let sourceAvailable = (function() {
Expand All @@ -260,7 +261,7 @@ const checkHasSuper = (function () {

if (sourceAvailable) {
return function checkHasSuper(func) {
return func.toString().indexOf('_super') > -1;
return HAS_SUPER_PATTERN.test(func.toString());
};
}

Expand Down
44 changes: 44 additions & 0 deletions packages/ember-runtime/tests/system/object/computed_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,47 @@ QUnit.test('list of properties updates when an additional property is added (suc

deepEqual(list.sort(), ['bar', 'foo', 'baz'].sort(), 'expected three computed properties');
});

QUnit.test('Calling _super in call outside the immediate function of a CP getter works', function() {
function macro(callback) {
return computed(function() {
return callback.call(this);
});
}

var MyClass = EmberObject.extend({
foo: computed(function() {
return 'FOO';
})
});

var SubClass = MyClass.extend({
foo: macro(function() {
return this._super();
})
});

ok(emberGet(SubClass.create(), 'foo'), 'FOO', 'super value is fetched');
});

QUnit.test('Calling _super in apply outside the immediate function of a CP getter works', function() {
function macro(callback) {
return computed(function() {
return callback.apply(this);
});
}

var MyClass = EmberObject.extend({
foo: computed(function() {
return 'FOO';
})
});

var SubClass = MyClass.extend({
foo: macro(function() {
return this._super();
})
});

ok(emberGet(SubClass.create(), 'foo'), 'FOO', 'super value is fetched');
});

0 comments on commit 64e40c2

Please sign in to comment.