Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX release] Ensure model can be observed by sync observers #18476

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/@ember/-internals/runtime/lib/mixins/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ export default Mixin.create({
@param {String} key The key to observe
@param {Object} target The target object to invoke
@param {String|Function} method The method to invoke
@param {Boolean} async Whether the observer is async or not
@param {Boolean} sync Whether the observer is sync or not
@return {Observable}
@public
*/
addObserver(key, target, method, async) {
addObserver(this, key, target, method, async);
addObserver(key, target, method, sync) {
addObserver(this, key, target, method, sync);
return this;
},

Expand All @@ -380,12 +380,12 @@ export default Mixin.create({
@param {String} key The key to observe
@param {Object} target The target object to invoke
@param {String|Function} method The method to invoke
@param {Boolean} async Whether the observer is async or not
@param {Boolean} sync Whether the observer is async or not
@return {Observable}
@public
*/
removeObserver(key, target, method, async) {
removeObserver(this, key, target, method, async);
removeObserver(key, target, method, sync) {
removeObserver(this, key, target, method, sync);
return this;
},

Expand Down
16 changes: 13 additions & 3 deletions packages/@ember/controller/lib/controller_mixin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Mixin, tracked } from '@ember/-internals/metal';
import { Mixin, computed } from '@ember/-internals/metal';
import { ActionHandler } from '@ember/-internals/runtime';
import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { symbol } from '@ember/-internals/utils';

const MODEL = symbol('MODEL');

/**
@module ember
Expand Down Expand Up @@ -43,5 +45,13 @@ export default Mixin.create(ActionHandler, {
@property model
@public
*/
model: EMBER_METAL_TRACKED_PROPERTIES ? tracked() : null,
model: computed({
get() {
return this[MODEL];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to make this non-enumerable if there is an easy way to do it, but if it's too annoying it's not a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way to do it currently is something I think we'd rather deprecate sooner rather than later (it requires some not-so-nice prototype chain manipulation), so I'd rather not for the moment

},

set(key, value) {
return (this[MODEL] = value);
},
}),
});
65 changes: 64 additions & 1 deletion packages/@ember/controller/tests/controller_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,70 @@ import Service, { inject as injectService } from '@ember/service';
import { Object as EmberObject } from '@ember/-internals/runtime';
import { Mixin, get } from '@ember/-internals/metal';
import { runDestroy, buildOwner } from 'internal-test-helpers';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { moduleFor, ApplicationTestCase, AbstractTestCase, runTask } from 'internal-test-helpers';
import { action } from '@ember/object';

moduleFor(
'Controller model',
class extends ApplicationTestCase {
async '@test model is tracked'() {
this.add(
'controller:index',
class extends Controller {
constructor() {
super(...arguments);
this.model = 0;
}

get derived() {
return this.model + 1;
}

@action
update() {
this.model++;
}
}
);

this.addTemplate('index', '<button {{on "click" this.update}}>{{this.derived}}</button>');

await this.visit('/');

this.assertText('1');

runTask(() => this.$('button').click());
this.assertText('2');
}

async '@test model can be observed with sync observers'(assert) {
let observerRunCount = 0;

this.add(
'controller:index',
class extends Controller {
constructor() {
super(...arguments);
this.model = 0;

this.addObserver('model', this, () => observerRunCount++, true);
}

@action
update() {
this.model++;
}
}
);

this.addTemplate('index', '<button {{on "click" this.update}}>{{this.model}}</button>');

await this.visit('/');
runTask(() => this.$('button').click());
assert.equal(observerRunCount, 1, 'observer ran exactly once');
}
}
);

moduleFor(
'Controller event handling',
Expand Down