-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from marko-js/issue-344
Fixes #344 - defineRenderer for marko
- Loading branch information
Showing
5 changed files
with
111 additions
and
1 deletion.
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,48 @@ | ||
var marko = require('./'); | ||
var createRenderFunc = require('raptor-renderer').createRenderFunc; | ||
|
||
function defineRenderer(def) { | ||
var template = def.template; | ||
var getTemplateData = def.getTemplateData; | ||
var renderer = def.renderer; | ||
|
||
var loadedTemplate; | ||
|
||
|
||
if (!renderer) { | ||
// Create a renderer function that takes care of translating | ||
// the input properties to a view state. Also, this renderer | ||
// takes care of re-using existing widgets. | ||
renderer = function renderer(input, out) { | ||
var newProps = input; | ||
|
||
if (!newProps) { | ||
// Make sure we always have a non-null input object | ||
newProps = {}; | ||
} | ||
|
||
if (!loadedTemplate) { | ||
// Lazily load the template on first render to avoid potential problems | ||
// with circular dependencies | ||
loadedTemplate = template.render ? template : marko.load(template); | ||
} | ||
|
||
// Use getTemplateData(state, props, out) to get the template | ||
// data. If that method is not provided then just use the | ||
// the state (if provided) or the input data. | ||
var templateData = getTemplateData ? | ||
getTemplateData(newProps, out) : | ||
newProps; | ||
|
||
// Render the template associated with the component using the final template | ||
// data that we constructed | ||
loadedTemplate.render(templateData, out); | ||
}; | ||
} | ||
|
||
renderer.render = createRenderFunc(renderer); | ||
|
||
return renderer; | ||
} | ||
|
||
module.exports = defineRenderer; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
Hello ${data.fullName}! | ||
</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,16 @@ | ||
exports.check = function(marko, markoCompiler, expect, done) { | ||
|
||
var defineRenderer = require('../../../../defineRenderer'); | ||
var renderer = defineRenderer({ | ||
template: require('./template.marko'), | ||
getTemplateData: function(input) { | ||
return { | ||
fullName: input.firstName + ' ' + input.lastName | ||
}; | ||
} | ||
}); | ||
|
||
var renderResult = renderer.render({ firstName: 'John', lastName: 'Doe' }); | ||
expect(renderResult.html).to.equal('<div>Hello John Doe!</div>'); | ||
done(); | ||
}; |