@@ -13,6 +13,17 @@ dust.onLoad = function(tmpl, cb) {
13
13
var app = express ( ) ;
14
14
15
15
var position ;
16
+ var context = {
17
+ wait : function ( chunk , context , bodies , params ) {
18
+ var delayMilliseconds = parseInt ( params . delay , 10 ) * 1000 ;
19
+ // Returning a Promise-- Dust will wait for the promise to resolve
20
+ var promise = q ( position ++ ) . delay ( delayMilliseconds ) ;
21
+ promise . then ( function ( position ) {
22
+ console . log ( 'Rendering' , params . name , 'which started in position' , position ) ;
23
+ } ) ;
24
+ return promise ;
25
+ }
26
+ } ;
16
27
17
28
app . get ( '/' , function ( req , res ) {
18
29
dust . render ( 'index' , { } , function ( err , out ) {
@@ -22,40 +33,22 @@ app.get('/', function (req, res) {
22
33
23
34
app . use ( function ( req , res , next ) {
24
35
console . log ( '\n\nBeginning the render of' , req . path ) ;
36
+ console . log ( 'Read hello.dust to see the block names and the order in which they appear.' ) ;
37
+ position = 1 ;
25
38
next ( ) ;
26
39
} ) ;
27
40
28
41
app . get ( '/streaming' , function ( req , res ) {
29
- position = 1 ;
30
- dust . stream ( 'hello' , {
31
- 'wait' : function ( chunk , context , bodies , params ) {
32
- var delayMilliseconds = parseInt ( params . delay , 10 ) * 1000 ;
33
- // Returning a Promise-- Dust will wait for the promise to resolve
34
- return q ( position ++ ) . delay ( delayMilliseconds )
35
- . then ( function ( position ) {
36
- console . log ( 'Rendering' , params . name , 'which started in position' , position ) ;
37
- } ) ;
38
- }
39
- } ) . pipe ( res )
42
+ dust . stream ( 'hello' , context ) . pipe ( res )
40
43
. on ( 'end' , function ( ) {
41
44
console . log ( 'Done!' ) ;
42
45
} ) ;
43
46
} ) ;
44
47
45
48
app . get ( '/rendering' , function ( req , res ) {
46
- position = 1 ;
47
- dust . render ( 'hello' , {
48
- 'wait' : function ( chunk , context , bodies , params ) {
49
- var delayMilliseconds = parseInt ( params . delay , 10 ) * 1000 ;
50
- // Returning a Promise-- Dust will wait for the promise to resolve
51
- return q ( position ++ ) . delay ( delayMilliseconds )
52
- . then ( function ( position ) {
53
- console . log ( 'Rendering' , params . name , 'which started in position' , position ) ;
54
- } ) ;
55
- }
56
- } , function ( err , out ) {
57
- console . log ( 'Done!' ) ;
49
+ dust . render ( 'hello' , context , function ( err , out ) {
58
50
res . send ( out ) ;
51
+ console . log ( 'Done!' ) ;
59
52
} ) ;
60
53
} ) ;
61
54
0 commit comments