This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7c2b113
Showing
6 changed files
with
163 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
bower_components |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = function(grunt) { | ||
require('load-grunt-tasks')(grunt); | ||
|
||
// Project configuration. | ||
grunt.initConfig({ | ||
pkg: grunt.file.readJSON('package.json'), | ||
|
||
simplemocha: { | ||
options: { | ||
globals:['should'], | ||
timeout: 3000, | ||
ignoreLeaks: false, | ||
ui: 'bdd', | ||
reporter: 'tap' | ||
}, | ||
all: { | ||
src: ['test/**/*.js'] | ||
} | ||
}, | ||
}); | ||
|
||
grunt.registerTask('test', ['simplemocha']); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
(function(dust) { | ||
var dust = require('dustjs-linkedin'); | ||
var stringify = require('json-stringify-pretty-compact'); | ||
require('dustjs-helpers'); | ||
|
||
dust.helpers.component = function component(chunk, context, bodies, params) { | ||
if (!params.name) { | ||
return chunk; | ||
} | ||
|
||
var originalContext = context; | ||
var thisContext; | ||
var template; | ||
var args = []; | ||
|
||
bodies = bodies || {}; | ||
params = params || {}; | ||
|
||
// Start with an empty context. | ||
thisContext = dust.makeBase(context.global); | ||
|
||
// Set the template to render. | ||
template = thisContext.templateName = params.name; | ||
|
||
// Handle params and bodies | ||
// | ||
// We evaluate these in the original context available to the component. | ||
// To accomplish this, we need to do some extra work. | ||
[bodies, params].forEach(function(obj, index) { | ||
for (var prop in obj) { | ||
var ret; | ||
|
||
// If the body or param is itself a Dust body (rather than a literal), | ||
// it is a function of the form fn(chunk, context). We wrap this | ||
// function with a new function which is bound to the dust body. When | ||
// it is evaluated, we make it use the original context available to the | ||
// include rather than the immediate context passed in by the caller. | ||
if (typeof obj[prop] === 'function') { | ||
ret = function(chunk) { | ||
return this(chunk, originalContext).data; | ||
}.bind(obj[prop]); | ||
} else { | ||
ret = obj[prop]; | ||
} | ||
|
||
args[prop] = ret; | ||
} | ||
}); | ||
|
||
// Push on the evaluated bodies and params. | ||
thisContext = thisContext.push(args); | ||
|
||
// Return the evaluated template. | ||
return dust.load(template, chunk, thisContext); | ||
} | ||
})(typeof exports !== 'undefined' ? module.exports = require('dustjs-linkedin') : dust); |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "dust-component-helper", | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Ryan L Frederick <[email protected]> (http://ryanfrederick.com/)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"dustjs-helpers": "^1.6.1", | ||
"dustjs-linkedin": "^2.6.1", | ||
"grunt": "^0.4.5", | ||
"grunt-simple-mocha": "^0.4.0", | ||
"json-stringify-pretty-compact": "^1.0.1", | ||
"load-grunt-tasks": "^3.1.0", | ||
"should": "~1.2.2" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<div class="c-example{?isFeatured} c--featured{/isFeatured}"> | ||
{body} | ||
{other} | ||
|
||
{! This should only print when the outer context is explicitly passed in. !} | ||
{?outer}<p>{outer}</p>{/outer} | ||
|
||
{! Only present when explicitly passed in !} | ||
{?state.faved}<p>Faved!</p>{/state.faved} | ||
</div> |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Declare deps. | ||
var dust = require('dustjs-linkedin'); | ||
var assert = require('assert'); | ||
var fs = require('fs'); | ||
var stringify = require('json-stringify-pretty-compact'); | ||
require('dustjs-helpers'); | ||
require('../index'); | ||
|
||
// Set up, compile and load the example component. | ||
var templateName = 'example'; | ||
var context = { | ||
outer: 'Outer', | ||
passed: 'Passed!', | ||
example: 'example', | ||
state: { | ||
faved: true | ||
} | ||
}; | ||
|
||
dust.compileFn(fs.readFileSync('test/fixtures/example.dust', 'utf8'), templateName); | ||
|
||
// Run the tests | ||
describe('component helper', function() { | ||
// Todo these two tests aren’t testing the helper: remove them | ||
it('can access keys in the current context.', function() { | ||
var code = '<p>{passed}</p>'; | ||
|
||
dust.renderSource(code, context, function(err, out) { | ||
assert.equal(out, '<p>Passed!</p>'); | ||
}); | ||
}); | ||
|
||
it('can render a template.', function() { | ||
dust.render(templateName, dust.makeBase({}), function(err, out) { | ||
assert.equal(out, '<div class="c-example"></div>'); | ||
}); | ||
}); | ||
|
||
// Proper tests | ||
it('exists.', function() { | ||
assert.equal(typeof dust.helpers.component, 'function'); | ||
}); | ||
|
||
it('has strong encapsulation by default.', function() { | ||
var code = '{@component name="example" /}'; | ||
|
||
dust.renderSource(code, context, function(err, out) { | ||
assert.equal(out, '<div class="c-example"></div>'); | ||
}); | ||
}); | ||
}); |