Reputation: 423
I am developing a web app using SlickGrid.
Our application has many grids in different tabs/pages and for that reason I am wondering if I can refactor some code so that I am not writing the same functions over and over for seperate grids.
An example: I need a custom Formatter since we have multiple tree grids (the formatter to implement the tree structure is identical for each grid.): function PercentCompleteFormatter(row, cell, value, columnDef, dataContext)
Given that I cannot pass the grid or a dataview as an arguement (assuming the signature above must be used for all custom formatter.), I basically have to have the same formatter code inside each code block where I am creating the grid.
Is there a way I can create one "generic" formatter and apply it to ALL grids?
The same question applies to things like sorting, filtering, etc... but I hope the above explaination covers what I'm trying to ask.
Thanks!
Upvotes: 0
Views: 838
Reputation: 9082
You can use jQuery's .proxy() helper (or similar) to bind a function to a particular context.
For example:
function MyFormatter(row, cell, value) {
var grid = this; // the grid will be the executing scope
// ...
}
var myFormatterForGridA = $.proxy(MyFormatter, gridA);
var myFormatterForGridB = $.proxy(MyFormatter, gridB);
Upvotes: 5