Skip to content

Commit

Permalink
Add transcluded scope to the tree node scope so the functions and var…
Browse files Browse the repository at this point in the history
…iables from such scope can be used in the node template (with $parent)
  • Loading branch information
christophercr committed May 29, 2016
1 parent 52d161f commit e367553
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/scripts/directives/ivh-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ angular.module('ivh.treeview').directive('ivhTreeview', ['ivhTreeviewMgr', funct
transcludedNodeTpl += (c.innerHTML || '').trim();
});
if(transcludedNodeTpl.length) {
transcludedScope = scope;
localOpts.nodeTpl = transcludedNodeTpl;
}

// regardless of whether or not the default or custom template is used
// transcluded scope should always be kept
transcludedScope = scope;
});

/**
Expand Down Expand Up @@ -349,6 +352,16 @@ angular.module('ivh.treeview').directive('ivhTreeview', ['ivhTreeviewMgr', funct
return localOpts.nodeTpl;
};

/**
* Get the tree node scope (transcluded scope)
*
* @return {Object} The node scope
* @private
*/
trvw.getTranscludedNodeScope = function() {
return transcludedScope;
};

/**
* Get the root of the tree
*
Expand Down
9 changes: 8 additions & 1 deletion src/scripts/services/ivh-treeview-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ angular.module('ivh.treeview').factory('ivhTreeviewCompiler', ['$compile', funct
}

var compiledContents;
var transcludedNodeScope;
return {
pre: (link && link.pre) ? link.pre : null,
/**
Expand All @@ -37,9 +38,15 @@ angular.module('ivh.treeview').factory('ivhTreeviewCompiler', ['$compile', funct
// Compile our template
if(!compiledContents) {
compiledContents = $compile(trvw.getNodeTpl());
transcludedNodeScope = trvw.getTranscludedNodeScope();
}

// the new node scope will prototypically inherit from the transcluded scope
// and it's parent will be the current treeview-node scope
var nodeScope = scope.$new(false, transcludedNodeScope);

// Add the compiled template
compiledContents(scope, function(clone) {
compiledContents(nodeScope, function(clone) {
element.append(clone);
});

Expand Down

1 comment on commit e367553

@christophercr
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solves the issue iVantage#81

Please sign in to comment.