From 9419f59f5e90b52759f6a99568e5f63832ac3af6 Mon Sep 17 00:00:00 2001 From: Godhuda Date: Tue, 2 Feb 2016 17:18:30 -0800 Subject: [PATCH] Integrate with Glimmer reference interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As part of this, we have stopped “borrowing” the glimmer `TestEnvironment` and started implementing the Ember-specific environment. --- .../lib/ember-metal-views/index.js | 5 +- packages/ember-glimmer/lib/environment.js | 61 +++++++++++++++++++ packages/ember-glimmer/lib/main.js | 1 + .../tests/integration/content-test.js | 61 +++++++++++++++++++ .../ember-glimmer/tests/utils/environment.js | 2 +- 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 packages/ember-glimmer/lib/environment.js diff --git a/packages/ember-glimmer/lib/ember-metal-views/index.js b/packages/ember-glimmer/lib/ember-metal-views/index.js index 86abd8a0d8b..8dd9352847d 100644 --- a/packages/ember-glimmer/lib/ember-metal-views/index.js +++ b/packages/ember-glimmer/lib/ember-metal-views/index.js @@ -1,3 +1,5 @@ +import { RootReference } from '../environment'; + export class Renderer { constructor(domHelper, { destinedForDOM, env } = {}) { this._dom = domHelper; @@ -6,9 +8,10 @@ export class Renderer { appendTo(view, target) { let env = this._env; + let self = new RootReference(view); env.begin(); - let result = view.template.render(view, env, { appendTo: target }); + let result = view.template.render(self, env, { appendTo: target }); env.commit(); // FIXME: Store this somewhere else diff --git a/packages/ember-glimmer/lib/environment.js b/packages/ember-glimmer/lib/environment.js new file mode 100644 index 00000000000..215a0a4b771 --- /dev/null +++ b/packages/ember-glimmer/lib/environment.js @@ -0,0 +1,61 @@ +import { Environment } from 'glimmer-runtime'; +import { get } from 'ember-metal/property_get'; + +// @implements PathReference +export class RootReference { + constructor(value) { + this._value = value; + } + + value() { + return this._value; + } + + isDirty() { + return true; + } + + get(propertyKey) { + return new PropertyReference(this, propertyKey); + } + + destroy() { + } +} + +// @implements PathReference +class PropertyReference { + constructor(parentReference, propertyKey) { + this._parentReference = parentReference; + this._propertyKey = propertyKey; + } + + value() { + return get(this._parentReference.value(), this._propertyKey); + } + + isDirty() { + return true; + } + + get(propertyKey) { + return new PropertyReference(this, propertyKey); + } + + destroy() { + } +} + +export default class extends Environment { + hasComponentDefinition() { + return false; + } + + hasHelper() { + return false; + } + + rootReferenceFor(value) { + return new RootReference(value); + } +} diff --git a/packages/ember-glimmer/lib/main.js b/packages/ember-glimmer/lib/main.js index e69de29bb2d..16e0ba23e31 100644 --- a/packages/ember-glimmer/lib/main.js +++ b/packages/ember-glimmer/lib/main.js @@ -0,0 +1 @@ +export { default as Environment } from 'ember-glimmer/environment'; \ No newline at end of file diff --git a/packages/ember-glimmer/tests/integration/content-test.js b/packages/ember-glimmer/tests/integration/content-test.js index da76cba8106..53ff48f7efb 100644 --- a/packages/ember-glimmer/tests/integration/content-test.js +++ b/packages/ember-glimmer/tests/integration/content-test.js @@ -1,5 +1,7 @@ import { RenderingTest, moduleFor } from '../utils/test-case'; import { set } from 'ember-metal/property_set'; +import { computed } from 'ember-metal/computed'; +import EmberObject from 'ember-runtime/system/object'; moduleFor('Static content tests', class extends RenderingTest { @@ -81,6 +83,65 @@ moduleFor('Dynamic content tests', class extends RenderingTest { this.assertSameNode(text1, text4); } + ['@test it can render a dynamic text node with deeply nested paths']() { + this.render('{{a.b.c.d.e.f}}', { + a: { b: { c: { d: { e: { f: 'hello' } } } } } + }); + let text1 = this.assertTextNode(this.firstChild, 'hello'); + + this.rerender(); + let text2 = this.assertTextNode(this.firstChild, 'hello'); + + this.assertSameNode(text1, text2); + + set(this.context, 'a.b.c.d.e.f', 'goodbye'); + + this.rerender(); + let text3 = this.assertTextNode(this.firstChild, 'goodbye'); + + this.assertSameNode(text1, text3); + + set(this.context, 'a.b.c.d.e.f', 'hello'); + + this.rerender(); + let text4 = this.assertTextNode(this.firstChild, 'hello'); + + this.assertSameNode(text1, text4); + } + + ['@test it can render a dynamic text node where the value is a computed property']() { + let Formatter = EmberObject.extend({ + formattedMessage: computed('message', function() { + return this.get('message').toUpperCase(); + }) + }); + + let m = Formatter.create({ message: 'hello' }); + + this.render('{{m.formattedMessage}}', { m }); + + let text1 = this.assertTextNode(this.firstChild, 'HELLO'); + + this.rerender(); + let text2 = this.assertTextNode(this.firstChild, 'HELLO'); + + this.assertSameNode(text1, text2); + + set(m, 'message', 'goodbye'); + + this.rerender(); + let text3 = this.assertTextNode(this.firstChild, 'GOODBYE'); + + this.assertSameNode(text1, text3); + + set(m, 'message', 'hello'); + + this.rerender(); + let text4 = this.assertTextNode(this.firstChild, 'HELLO'); + + this.assertSameNode(text1, text4); + } + ['@test it can render a dynamic element']() { this.render('

{{message}}

', { message: 'hello' diff --git a/packages/ember-glimmer/tests/utils/environment.js b/packages/ember-glimmer/tests/utils/environment.js index 3df9834729c..aa4b1a74594 100644 --- a/packages/ember-glimmer/tests/utils/environment.js +++ b/packages/ember-glimmer/tests/utils/environment.js @@ -1 +1 @@ -export { TestEnvironment as default } from 'glimmer-test-helpers'; +export { Environment as default } from 'ember-glimmer';