angular-ivh-treeview
Version:
Treeview for angular with filtering and checkboxes
55 lines (50 loc) • 1.7 kB
JavaScript
/**
* Compile helper for treeview nodes
*
* Defers compilation until after linking parents. Otherwise our treeview
* compilation process would recurse indefinitely.
*
* Thanks to http://stackoverflow.com/questions/14430655/recursion-in-angular-directives
*
* @private
* @package ivh.treeview
* @copyright 2014 iVantage Health Analytics, Inc.
*/
angular.module('ivh.treeview').factory('ivhTreeviewCompiler', ['$compile', function($compile) {
'use strict';
return {
/**
* Manually compiles the element, fixing the recursion loop.
* @param {Object} element The angular element or template
* @param {Function} link [optional] A post-link function, or an object with function(s) registered via pre and post properties.
* @returns An object containing the linking functions.
*/
compile: function(element, link) {
// Normalize the link parameter
if(angular.isFunction(link)) {
link = { post: link };
}
var compiledContents;
return {
pre: (link && link.pre) ? link.pre : null,
/**
* Compiles and re-adds the contents
*/
post: function(scope, element, attrs, trvw) {
// Compile our template
if(!compiledContents) {
compiledContents = $compile(trvw.getNodeTpl());
}
// Add the compiled template
compiledContents(scope, function(clone) {
element.append(clone);
});
// Call the post-linking function, if any
if(link && link.post) {
link.post.apply(null, arguments);
}
}
};
}
};
}]);