I am trying to retrieve values in a loop from the parent context. Dust is able to call my method, but my method is not able to reach my properties which I have defined in the class, due to the context is the current item.
So for example:
JavaScript class
MyNamespace.MyClass = function () {
this.names= ["Me", "You", "StackOverflow"];
this.isAwesome = true;
};
MyNamespace.MyClass.prototype.sayHello = function() {
if(this.isAwesome) {
return "Hello awesome ";
// This code will never be reached due to 'this' is one of the items from the array
}
return "Hello ";
};
My Dust
{#names}{sayHello} {.}{~n}{/names}
Problem
The sayHello
method will be called as expected but the context is the current item instead of the parent. So this
is equals to the current item and of course "Me".isAwesome
will always return false. As you would understand, I would like to be awesome.
Question
How can I achieve that 3 awesome people are returned by dust.
When you call a function using Dust, the function is invoked with the arguments
chunk, context, bodies, params
.You can call
context.get(name)
to search the context stack for your desired variable.