Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
render function now fetches templates
- Loading branch information
David Byrd
committed
May 9, 2013
1 parent
7d39c54
commit dce3866
Showing
1 changed file
with
30 additions
and
2 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 |
---|---|---|
@@ -1,6 +1,34 @@ | ||
var mustache = require('ender-mustache') | ||
, loadTemplate = function (path, callback) { //TODO figure out how to do this without an extra request | ||
$.ajax({ | ||
url: path | ||
, type: 'html' | ||
, success: function (res) { | ||
callback(res) | ||
} | ||
}) | ||
} | ||
|
||
|
||
|
||
ender.ender({ | ||
render: mustache.render, | ||
mustache: mustache | ||
/** | ||
* Either returns rendered HTML or inserts it into a parent element. | ||
* | ||
* @param {string} path on current domain to template. | ||
* @param {Object} data that the template requires. | ||
* @param {string} jQuery style selector for the parent | ||
* element that the rendered template should be inserted into. | ||
*/ | ||
render: function (pathToTemplate, data, parentSelector) { | ||
loadTemplate(pathToTemplate, function (template) { | ||
parentSelector ? | ||
Array.prototype.slice.call(document.querySelectorAll(parentSelector)).forEach(function (el){ | ||
el.innerHTML = $.render(template, data) | ||
}) | ||
: return $.render(template, data) | ||
}) | ||
|
||
} | ||
, mustache: mustache | ||
}) |