Skip to content

Commit

Permalink
fix(ngTableController): reference to $column not always available in …
Browse files Browse the repository at this point in the history
…$column getter functions

BREAKING CHANGE:

A context object combines and replaces the `$scope` and `locals` argument originally supplied to
`$column` getter functions.

This context object prototypically inherits from the original `$scope` and has the fields from the
original `locals` argument as own properties.

**It change is very unlikely to affect you**

`ngTableColumn.buildColumn` now expects a third parameter - a reference to the `$columns`
array that will contain the column being built
  • Loading branch information
ccrowhurstram committed Sep 15, 2015
1 parent 103b2be commit adddb27
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/scripts/ngTable.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@
}

var localValue;
var getter = function (scope, locals) {
var getter = function (context) {
if (localValue !== undefined){
return localValue;
}
return $parse(expr)(scope, angular.extend(locals || {}, {
$columns: columns
}));
return $parse(expr)(context);
};
getter.assign = function($scope, value){
var parsedExpr = $parse(expr);
Expand Down
16 changes: 10 additions & 6 deletions src/scripts/ngTableColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
*
* @param {Object} column an existing $column or simple column data object
* @param {Scope} defaultScope the $scope to supply to the $column getter methods when not supplied by caller
* @param {Array} columns a reference to the columns array to make available on the context supplied to the
* $column getter methods
* @returns {Object} a $column object
*/
function buildColumn(column, defaultScope){
function buildColumn(column, defaultScope, columns){
// note: we're not modifying the original column object. This helps to avoid unintended side affects
var extendedCol = Object.create(column);
var defaults = createDefaults();
Expand All @@ -78,11 +80,13 @@
// satisfy the arguments expected by the function returned by parsedAttribute in the ngTable directive
var getterFn = extendedCol[prop1];
extendedCol[prop1] = function () {
if (arguments.length === 0) {
return getterFn.call(column, defaultScope);
} else {
return getterFn.apply(column, arguments);
}
var scope = arguments[0] || defaultScope;
var context = Object.create(scope);
angular.extend(context, {
$column: extendedCol,
$columns: columns
});
return getterFn.call(column, context);
};
if (getterFn.assign){
extendedCol[prop1].assign = getterFn.assign;
Expand Down
14 changes: 7 additions & 7 deletions src/scripts/ngTableController.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@
this.loadFilterData = function ($columns) {
angular.forEach($columns, function ($column) {
var result;
result = $column.filterData($scope, {
$column: $column
});
result = $column.filterData($scope);
if (!result) {
delete $column.filterData;
return;
Expand Down Expand Up @@ -141,9 +139,11 @@
};

this.buildColumns = function (columns) {
return columns.map(function(col){
return ngTableColumn.buildColumn(col, $scope)
})
var result = [];
columns.forEach(function(col){
result.push(ngTableColumn.buildColumn(col, $scope, result));
});
return result
};

this.parseNgTableDynamicExpr = function (attr) {
Expand Down Expand Up @@ -218,7 +218,7 @@
if (!$scope.$columns) return false;

return some($scope.$columns, function($column){
return $column.filter($scope, { $column: $column});
return $column.filter($scope);
});
}

Expand Down
11 changes: 5 additions & 6 deletions test/tableDynamicSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe('ng-table-dynamic', function() {
'</table>' +
'</div>');

function getCustomClass(parmasScope){
if (parmasScope.$column.title().indexOf('Money') !== -1){
function getCustomClass(context){
if (context.$column.title().indexOf('Money') !== -1){
return 'moneyHeaderClass';
} else{
return 'customClass';
Expand Down Expand Up @@ -534,11 +534,10 @@ describe('ng-table-dynamic', function() {
beforeEach(inject(function($compile, NgTableParams) {

ageFilter = { age: 'text'};
function getFilter(paramsScope, locals){
var $column = (paramsScope.$column || locals.$column);
if ($column.title() === 'Name of user') {
function getFilter(paramsScope){
if (paramsScope.$column.title() === 'Name of user') {
return {username: 'text'};
} else if ($column.title() === 'Age') {
} else if (paramsScope.$column.title() === 'Age') {
return ageFilter;
}
}
Expand Down

0 comments on commit adddb27

Please sign in to comment.