Skip to content

Commit 1ceca3e

Browse files
committed
template render takes template obj; tests
1 parent 00e2788 commit 1ceca3e

File tree

6 files changed

+107
-25
lines changed

6 files changed

+107
-25
lines changed

packages/spark/patch_tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Tinytest.add("spark - patcher - basic", function(test) {
1+
Tinytest.add("spark - patch - basic", function(test) {
22

33
var Patcher = Spark._Patcher;
44

@@ -120,7 +120,7 @@ Tinytest.add("spark - patcher - basic", function(test) {
120120
_.each([["aaa","zzz"], ["",""], ["aaa",""], ["","zzz"]], rangeTest);
121121
});
122122

123-
Tinytest.add("spark - patcher - copyAttributes", function(test) {
123+
Tinytest.add("spark - patch - copyAttributes", function(test) {
124124

125125
var attrTester = function(tagName, initial) {
126126
var node;

packages/spark/spark.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
// timer' button again. the problem is almost certainly in atFlushTime
3030
// (not hard to see what it is.)
3131

32-
// XXX align the arguments to the render() callback with the arguments
33-
// to events (instead of landmark, pass the 'template' object again?
34-
// that suggess that david is right and startNode() should be a
35-
// function.)
36-
3732
(function() {
3833

3934
Spark = {};

packages/spark/spark_tests.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ Tinytest.add("spark - basic isolate", function (test) {
262262
Meteor.flush();
263263
test.equal(div.html(), '<div><span>baz</span></div>');
264264

265+
div.kill();
266+
Meteor.flush();
265267
});
266268

267269
Tinytest.add("spark - one render", function (test) {
@@ -1300,6 +1302,9 @@ Tinytest.add("spark - labeled landmarks", function (test) {
13001302
excludeLandmarks[5].set(false);
13011303
Meteor.flush();
13021304
expect(["c", 5, "r", 5, "r", 4, "r", 3], [108, 108, 107, 105]);
1305+
1306+
div.kill();
1307+
Meteor.flush();
13031308
});
13041309

13051310

@@ -1778,6 +1783,8 @@ Tinytest.add("spark - landmark constant", function(test) {
17781783
// can patch here, renderCount stays the same
17791784
test.equal(renderCount, 2);
17801785

1786+
div.kill();
1787+
Meteor.flush();
17811788
});
17821789

17831790

@@ -2928,6 +2935,8 @@ Tinytest.add("spark - isolate inside landmark", function (test) {
29282935
var foo2 = d.node().firstChild;
29292936
test.equal(d.node().lastChild.nodeValue, '2');
29302937
test.isTrue(foo1 === foo2);
2938+
d.kill();
2939+
Meteor.flush();
29312940

29322941
// test that selectors in a landmark preservation map are resolved
29332942
// relative to the landmark, not relative to the re-rendered
@@ -2952,7 +2961,8 @@ Tinytest.add("spark - isolate inside landmark", function (test) {
29522961
var foo2 = DomUtils.find(d.node(), '.foo');
29532962
test.equal(foo2.nextSibling.nodeValue, '2');
29542963
test.isTrue(foo1 === foo2);
2955-
2964+
d.kill();
2965+
Meteor.flush();
29562966
});
29572967

29582968
Tinytest.add("spark - nested onscreen processing", function (test) {
@@ -3227,6 +3237,9 @@ Tinytest.add("spark - find/findAll on landmark", function (test) {
32273237
Meteor.flush();
32283238
check(false);
32293239
check(true);
3240+
3241+
d.kill();
3242+
Meteor.flush();
32303243
});
32313244

32323245
Tinytest.add("spark - landmark clean-up", function (test) {
@@ -3327,3 +3340,35 @@ Tinytest.add("spark - bubbling render", function (test) {
33273340
});
33283341

33293342
})();
3343+
3344+
Tinytest.add("spark - landmark arg", function (test) {
3345+
var div = OnscreenDiv(Spark.render(function () {
3346+
return Spark.createLandmark({
3347+
render: function () {
3348+
var landmark = this;
3349+
landmark.firstNode().innerHTML = 'Greetings';
3350+
landmark.lastNode().innerHTML = 'Line';
3351+
landmark.find('i').innerHTML =
3352+
(landmark.findAll('b').length)+"-bold";
3353+
}
3354+
}, function () {
3355+
return Spark.attachEvents({
3356+
'click': function (event, landmark) {
3357+
landmark.firstNode().innerHTML = 'Hello';
3358+
landmark.lastNode().innerHTML = 'World';
3359+
landmark.find('i').innerHTML =
3360+
(landmark.findAll('*').length)+"-element";
3361+
}
3362+
}, '<b>Foo</b> <i>Bar</i> <u>Baz</u>');
3363+
});
3364+
}));
3365+
3366+
test.equal(div.text(), "Foo Bar Baz");
3367+
Meteor.flush();
3368+
test.equal(div.text(), "Greetings 1-bold Line");
3369+
clickElement(DomUtils.find(div.node(), 'i'));
3370+
test.equal(div.text(), "Hello 3-element World");
3371+
3372+
div.kill();
3373+
Meteor.flush();
3374+
});

packages/templating/deftemplate.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@
4545
// create/render/destroy callbacks on templates
4646
var templateInstanceData = {};
4747

48+
var templateObjFromLandmark = function (landmark) {
49+
var template = {
50+
find: function (selector) {
51+
return landmark.find(selector);
52+
},
53+
findAll: function (selector) {
54+
return landmark.findAll(selector);
55+
},
56+
firstNode: landmark.firstNode(),
57+
lastNode: landmark.lastNode(),
58+
data: templateInstanceData[landmark.id]
59+
};
60+
return template;
61+
};
62+
4863
Meteor._def_template = function (name, raw_func) {
4964
Meteor._hook_handlebars();
5065

@@ -64,7 +79,8 @@
6479
},
6580
render: function () {
6681
tmpl.render &&
67-
tmpl.render.call(templateInstanceData[this.id], this);
82+
tmpl.render.call(templateInstanceData[this.id],
83+
templateObjFromLandmark(this));
6884
},
6985
destroy: function () {
7086
tmpl.destroy &&
@@ -89,18 +105,8 @@
89105
var newEventMap = {};
90106
_.each(oldEventMap, function (handler, key) {
91107
newEventMap[key] = function (event, landmark) {
92-
var template = {
93-
find: function (selector) {
94-
return landmark.find(selector);
95-
},
96-
findAll: function (selector) {
97-
return landmark.findAll(selector);
98-
},
99-
firstNode: landmark.firstNode(),
100-
lastNode: landmark.lastNode(),
101-
data: templateInstanceData[landmark.id]
102-
};
103-
return handler.call(this, event, template);
108+
return handler.call(this, event,
109+
templateObjFromLandmark(landmark));
104110
};
105111
});
106112
return newEventMap;

packages/templating/templating_tests.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,7 @@
230230
{{/isolate}}
231231
{{/isolate}}
232232
</template>
233+
234+
<template name="test_template_arg_a">
235+
<b>Foo</b> <i>Bar</i> <u>Baz</u>
236+
</template>

packages/templating/templating_tests.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,9 @@ Tinytest.add("templating - matching in list", function (test) {
502502
var buf = [];
503503
_.extend(Template.test_listmatching_a1, {
504504
create: function () { buf.push('+'); },
505-
render: function (landmark) {
506-
var letter = DomUtils.rangeToHtml(landmark.firstNode(),
507-
landmark.lastNode()).match(/\S+/)[0];
505+
render: function (template) {
506+
var letter = DomUtils.rangeToHtml(template.firstNode,
507+
template.lastNode).match(/\S+/)[0];
508508
buf.push('*'+letter);
509509
},
510510
destroy: function () { buf.push('-'); }
@@ -567,4 +567,36 @@ Tinytest.add("templating - isolate helper", function (test) {
567567
div.kill();
568568
Meteor.flush();
569569

570-
});
570+
});
571+
572+
Tinytest.add("templating - template arg", function (test) {
573+
Template.test_template_arg_a.events = {
574+
click: function (event, template) {
575+
template.firstNode.innerHTML = 'Hello';
576+
template.lastNode.innerHTML = 'World';
577+
template.find('i').innerHTML =
578+
(template.findAll('*').length)+"-element";
579+
template.lastNode.innerHTML += ' (the secret is '+
580+
template.data.secret+')';
581+
}
582+
};
583+
584+
Template.test_template_arg_a.render = function (template) {
585+
template.firstNode.innerHTML = 'Greetings';
586+
template.lastNode.innerHTML = 'Line';
587+
template.find('i').innerHTML =
588+
(template.findAll('b').length)+"-bold";
589+
template.data.secret = "strawberry pie";
590+
};
591+
592+
var div = OnscreenDiv(Spark.render(Template.test_template_arg_a));
593+
594+
test.equal(div.text(), "Foo Bar Baz");
595+
Meteor.flush();
596+
test.equal(div.text(), "Greetings 1-bold Line");
597+
clickElement(DomUtils.find(div.node(), 'i'));
598+
test.equal(div.text(), "Hello 3-element World (the secret is strawberry pie)");
599+
600+
div.kill();
601+
Meteor.flush();
602+
});

0 commit comments

Comments
 (0)