BorisMoore / jquery-tmpl

The original official jQuery Templates plugin. This project was maintained by the jQuery team as an official jQuery plugin. It is no longer in active development, and has been superseded by JsRender.
3.23k stars 1.01k forks source link

Direct access to functions defined on data object #110

Closed heyotwell closed 13 years ago

heyotwell commented 13 years ago

I'd like to be able to use functions attached to my data objects like this:

var tmplTestObject = {
    url: "http://www.example.com",
    firstName: "Joe",
    lastName: "Smiith",
    getName: function() {
        return this.firstName + " " + this.lastName;
    }
};

To access the getName() function in a template, I have to refer to it by $item.data.getName() or $data.getName() format, though other attributes are available by ${firstName} syntax. Is this correct?

BorisMoore commented 13 years ago

${getName()} will call the function, but the context of the call will not be $data, so the 'this' will not be the data object, but the templateItem, so you would need to do this.data.firstName in the function. If you are using this.firstName (or similar) in the function, then, yes, ${$data.getName()} is the right way to call the function, so that the 'this' context it the data item.