-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow promise resolution in Dust global helpers
- Loading branch information
1 parent
519526f
commit ebb9985
Showing
3 changed files
with
38 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,21 @@ | ||
(function (root, factory) { | ||
if (typeof exports === 'object') { | ||
factory(require('../../../lib/dust')); | ||
factory(require('../../../lib/dust'), require('ayepromise')); | ||
} else { | ||
factory(root.dust); | ||
factory(root.dust, ayepromise); | ||
} | ||
}(this, function(dust) { | ||
dust.testHelpers = {}; | ||
dust.testHelpers.tap = function(input, chunk, context) { | ||
// return given input if there is no dust reference to resolve | ||
var output = input; | ||
// dust compiles a string/reference such as {foo} to function, | ||
if (typeof input === 'function') { | ||
// just a plain function (a.k.a anonymous functions) in the context, not a dust `body` function created by the dust compiler | ||
if (input.isFunction === true) { | ||
output = input(); | ||
} else { | ||
output = ''; | ||
chunk.tap(function(data){ | ||
output += data; | ||
return ''; | ||
}).render(input, context).untap(); | ||
if (output === '') { | ||
output = false; | ||
} | ||
} | ||
} | ||
return output; | ||
}; | ||
|
||
}(this, function(dust, ayepromise) { | ||
dust.helpers = {}; | ||
dust.helpers.error = function(chunk, context, bodies, params) { | ||
throw params.errorMessage; | ||
}; | ||
dust.helpers.promise = function(chunk, context, bodies, params) { | ||
var defer = ayepromise.defer(); | ||
if (params.reject) { | ||
defer.reject(params.reject); | ||
} else { | ||
defer.resolve(params.resolve); | ||
} | ||
return defer.promise; | ||
}; | ||
})); |