Skip to content
This repository was archived by the owner on Apr 4, 2019. It is now read-only.

Commit 95e6357

Browse files
Tom Dale and Yehuda Katztilde-engineering
Tom Dale and Yehuda Katz
authored andcommitted
Encapsulate state needed for re-render
This commit introduces the notion of a “result” data type, which encapsulates all of the state needed to be passed to a compiled template in order to successfully re-render with an existing element. Rather than returning a document fragment, `render()` now returns this result type. For compatibility, consumers of this API will need to use the `fragment` property of the result rather than the result directly. In order to re-render, you can pass the last result as the `lastResult` option, and it will use that element and morph to update. One small additional change: contextual elements must now be passed in the options hash. We decided to make this change as the list of arguments was growing unwieldy, and we wanted to make this durable to changes in the future. We will also investigate incorporating block params into the options hash.
1 parent 4a2362c commit 95e6357

File tree

5 files changed

+75
-71
lines changed

5 files changed

+75
-71
lines changed

packages/htmlbars-compiler/lib/hydration-javascript-compiler.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ prototype.compile = function(opcodes, options) {
3030

3131
processOpcodes(this, opcodes);
3232

33-
var i, l, morphs;
33+
var i, l;
3434

3535
var indent = this.indent + ' ';
3636

37+
var morphs = indent+'var morphs;\n';
38+
3739
if (this.morphs.length) {
38-
morphs =
39-
indent+'var morphs = env.morphs;\n' +
40+
morphs +=
4041
indent+'if (!morphs) {\n' +
4142
indent+' morphs = new Array(' + this.morphs.length + ');\n';
4243

@@ -46,8 +47,6 @@ prototype.compile = function(opcodes, options) {
4647
}
4748

4849
morphs += indent+'}\n';
49-
} else {
50-
morphs = indent+'var morphs;\n';
5150
}
5251

5352
this.source.unshift(morphs);

packages/htmlbars-compiler/lib/template-compiler.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ TemplateCompiler.prototype.endProgram = function(program, programDepth) {
8383

8484
var blockParams = program.blockParams || [];
8585

86-
var templateSignature = 'context, env, contextualElement';
86+
var templateSignature = 'context, env, options';
8787
if (blockParams.length > 0) {
8888
templateSignature += ', blockArguments';
8989
}
@@ -100,8 +100,11 @@ TemplateCompiler.prototype.endProgram = function(program, programDepth) {
100100
indent+' render: function render(' + templateSignature + ') {\n' +
101101
indent+' var dom = env.dom;\n' +
102102
this.getHydrationHooks(indent + ' ', this.hydrationCompiler.hooks) +
103+
indent+' var contextualElement = options && options.contextualElement;\n' +
104+
indent+' var lastResult = options && options.lastResult;\n' +
103105
indent+' dom.detectNamespace(contextualElement);\n' +
104-
indent+' var fragment = env.target;\n' +
106+
indent+' var fragment = lastResult ? lastResult.fragment : null;\n' +
107+
indent+' var morphs = lastResult ? lastResult.morphs : null;\n' +
105108
indent+' if (!fragment && env.useFragmentCache && dom.canClone) {\n' +
106109
indent+' if (this.cachedFragment === null) {\n' +
107110
indent+' fragment = this.build(dom);\n' +
@@ -118,7 +121,7 @@ TemplateCompiler.prototype.endProgram = function(program, programDepth) {
118121
indent+' fragment = this.build(dom);\n' +
119122
indent+' }\n' +
120123
hydrationProgram +
121-
indent+' return { fragment: fragment, morphs: morphs };\n' +
124+
indent+' return lastResult || { fragment: fragment, morphs: morphs };\n' +
122125
indent+' }\n' +
123126
indent+' };\n' +
124127
indent+'}())';

0 commit comments

Comments
 (0)