Skip to content

Commit f1a22b1

Browse files
committed
discovered var was, in fact, functioning aside from that edge case
1 parent 2ad211f commit f1a22b1

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ Logic
4949
Just write javascript. Really; write _any_ javascript inline with your markup and it just works (tm).
5050

5151
Known exceptions to 'just working':
52-
`var` statements do bad things (like write tags), since they try to bind to the context. As a workaround (until I start
53-
using the new Proxy API - _and_ `node` supports the new Proxy API), store all scratch variables on $scope (if you're
54-
using an explicit context, you can pass a different identifier as the first argument to the context-generator,
55-
if $scope is too long/misleading for you).
52+
`var` statements don't function immediately within an explicitly defined context.
53+
Make an IIFE within the explicit with (like in the first test) to get around the problem,
54+
or use implied contexts. Or don't use locals.
5655

5756
Scope
5857
-------
@@ -109,7 +108,7 @@ module.exports = function() {
109108
});
110109
};
111110
```
112-
Since `http` (and `sink`, and `JSON`) will be overridden by the implicit context. You have two options:
111+
Since `http` (and `JSON`) will be overridden by the implicit context. You have two options:
113112
- Export the desired functions into the `$scope`
114113
- Utilize _explicit contexts_ for complicated files
115114

@@ -173,36 +172,36 @@ Scope object passed in the options object:
173172
`not.js` File:
174173
```js
175174
module.exports = function() {
176-
$scope.promise = new $scope.Promise(function(resolve, reject) {
177-
$scope.http.get('www.github.com', function(cli) {
178-
$scope.sink = '';
175+
var promise = new $scope.Promise(function(resolve, reject) {
176+
$scope.http.get('http://www.github.com', function(cli) {
177+
var sink = '';
179178
cli.on('data', function(data) {
180-
$scope.sink += data;
179+
sink += data;
181180
});
182181
cli.on('end', function() {
183182
$(sink, true);
184183
resolve(); //The return value of implied functions isn't actually used
185184
});
186185
cli.on('error', function(e) {
187-
$scope.error = $scope.JSON.parse(e);
186+
var error = $scope.JSON.parse(e);
188187
html
189188
head
190-
meta({title: $scope.error})
189+
meta({title: error})
191190
$head
192191
body
193-
$($scope.error);
192+
$(error);
194193
$body
195194
$html
196195
resolve();
197196
});
198197
});
199198
});
200199

201-
return $scope.promise //Implied functions that return promises have their .proxy attribute
202-
//set to the proxy generated for them - call .collect() on the proxy
203-
//to get the result string when the promise resolves
204-
//eg. promise.then(function() { return promise.proxy.collect(); })
205-
// .then(function(rendered) {doStuff(rendered);});
200+
return promise; //Implied functions that return promises have their .proxy attribute
201+
//set to the proxy generated for them - call .collect() on the proxy
202+
//to get the result string when the promise resolves
203+
//eg. promise.then(function() { return promise.proxy.collect(); })
204+
// .then(function(rendered) {doStuff(rendered);});
206205
};
207206
```
208207

@@ -219,5 +218,4 @@ TODO
219218
====
220219
Doctype shorthand?
221220
Shorthand for inlining a script (something shorter than `script({type: 'text/javascript'}); $($scope.func.toString(), true); script`)
222-
Figure out how to fix 'var' within the dsl sensibly. Likely involves some metatrickery with the correct flow of calls through the proxy object chain (just overriding `get` hasn't worked, in my experience). It's complicated by variable hoisting.
223221
Benchmarks?

test/examples.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,34 @@ describe('the third functional example in the readme', function() {
7777
it('should run and produce correct html', function(done) {
7878
this.timeout(5000);
7979

80-
var implicitExample = function() {
81-
$scope.promise = new $scope.Promise(function(resolve, reject) {
82-
$scope.http.get('http://www.github.com', function(cli) {
83-
$scope.sink = '';
84-
cli.on('data', function(data) {
85-
$scope.sink += data;
86-
});
87-
cli.on('end', function() {
88-
$($scope.sink, true);
89-
$scope.log('Wrote result.')
90-
resolve(); //The return value of implied functions isn't actually used
91-
});
92-
cli.on('error', function(e) {
93-
$scope.error = $scope.JSON.parse(e);
94-
html
95-
head
96-
meta({title: $scope.error})
97-
$head
98-
body
99-
$($scope.error);
100-
$body
101-
$html
102-
resolve();
103-
});
80+
var implicitExample = function() {
81+
var promise = new $scope.Promise(function(resolve, reject) {
82+
$scope.http.get('http://www.github.com', function(cli) {
83+
var sink = '';
84+
cli.on('data', function(data) {
85+
sink += data;
86+
});
87+
cli.on('end', function() {
88+
$(sink, true);
89+
resolve(); //The return value of implied functions isn't actually used
90+
});
91+
cli.on('error', function(e) {
92+
var error = $scope.JSON.parse(e);
93+
html
94+
head
95+
meta({title: error})
96+
$head
97+
body
98+
$(error);
99+
$body
100+
$html
101+
resolve();
104102
});
105103
});
104+
});
106105

107-
return $scope.promise;
108-
};
106+
return promise;
107+
};
109108

110109
var result = notjs.renderFunc(implicitExample, {
111110
Promise: require('promise'),

test/html.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ describe('the not.js html dsl', function() {
1414
items: ['Foo', 'Bar', 'Baz', 'Bat']
1515
}
1616
};
17+
1718
//Call the context with the name you want the scope accessible by and the scope object itself
1819
//"$scope" is the default and may be omitted
1920
with (context('$scope', scope)) {
20-
21+
(function() { //Make a closure so we can use locals
2122
//Any valid identifier is a used as a tag (with some exceptions, see below)
2223
html
2324
head
@@ -47,9 +48,9 @@ describe('the not.js html dsl', function() {
4748
$p
4849

4950
//Include plain js logic inline with your template!
50-
for ($scope.i=0; $scope.i<4; $scope.i++) {
51+
for (var i=0; i<4; i++) {
5152
p
52-
$('I\'m number '+$scope.i+'!');
53+
$('I\'m number '+i+'!');
5354
$p
5455
}
5556

@@ -59,6 +60,7 @@ describe('the not.js html dsl', function() {
5960
$($scope.impliedContext($scope.sublayout), true)
6061
$body
6162
$html
63+
})();
6264
}
6365
context.collect().should.be.equal(
6466
'<html><head><meta title=\"not.js wat?\" /></head><body style=\"width: 100%;\"><p><h1 style=\"text-align: right;\">Title '+
@@ -95,7 +97,7 @@ describe('the not.js html dsl', function() {
9597
var path = require('path').resolve('test', 'implied.not.js');
9698
notjs.renderPath(path, {scope: scope}, function(err, res) {
9799
if (err) throw err;
98-
res.should.be.equal('<h1>Title!</h1><ul class=\"un-list\"><li>Item: Foo</li><li>Item: Bar</li><li>Item: Baz</li><li>Item: Bat</li></ul>');
100+
res.should.be.equal('<h1>Title!</h1><ul class="un-list"><li>Item: Foo</li><li>Item: Bar</li><li>Item: Baz</li><li>Item: Bat</li></ul>');
99101
done();
100102
});
101103
});
@@ -114,9 +116,13 @@ describe('the not.js html dsl', function() {
114116
it('lets you alias the scope in implied arguments by taking an argument', function() {
115117
var block = function(s) {
116118
h1; $('text: '); $(s.text); $h1;
119+
120+
var example = 'thing';
121+
example += ' and more';
122+
h2; $(example); $h2;
117123
};
118124

119125
var result = notjs.renderFunc(block, {text: 'some text'});
120-
result.should.be.equal('<h1>text: some text</h1>');
126+
result.should.be.equal('<h1>text: some text</h1><h2>thing and more</h2>');
121127
});
122128
});

0 commit comments

Comments
 (0)