linkedin/dustjs — pull request #117 by wshaver (2012-08-16 17:57:24+00:00)
Title: Global and deep stack context functions have wrong 'this' pointer.

Related to: https://github.com/linkedin/dustjs/pull/96 (20 days previous to this post)

The fix in this pull request always uses the 'current context'
```javascript
elem = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]);
```
The testing framework doesn't allow for more complicated contexts to be easily defined as it expects a simple single-stack context. Consider the following more complicated context and global stack:

```javascript
var context = dust.makeBase({
name: '1Bob',
getName1: function(){ return this.name;}});
var instance = context.push({
name: '2Joe',
getName2: function(){ return this.name;}});
instance = instance.push({
name: '3Tim',
getName3: function(){ return this.name;}});
```
With the dust template:

```html
{getName1}
{getName2}
{getName3}
````

The current implementation returns:
3Bob3Bob3Bob

Please indicate where and how to put a test with a more complicated context and I'll be happy to add one.

To solve this issue, we need to bind the function at compile time. Using underscore (just to get it done quickly) the code looks like this:

```javascript
Context.prototype.get = function(key) {
var ctx = this.stack, value;
while(ctx) {
if (ctx.isObject) {
value = ctx.head[key];
if(typeof value === 'function'){
value = _.bind(value, ctx.head);
}
if (value !== undefined) {
return value;
}
}
ctx = ctx.tail;
}
if(this.global){
value = this.global[key];
if(typeof value === 'function'){
value = _.bind(value, this.global);
}
return value;
}
return undefined;
};
```

This fixes the global / deep stack issue and passes the unit tests defined in pull request 96. I've implemented a minimal 'bind' inside of the .git function. Please tell me where to add tests for this issue. I've added some manual tests in a site I'm building, but would like to add tests in the linkedin project.