Skip to content

Commit 5160b80

Browse files
committed
fix(uiGridRow): Use promise to get compiled elm fn
We need to use a promise to get the compiled element function for attaching row elements. If the template fetch is deferred then the compiled template function will be deferred as well.
1 parent ed95cfe commit 5160b80

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

src/js/core/directives/ui-grid-cell.js

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ angular.module('ui.grid').directive('uiGridCell', ['$compile', '$parse', 'gridUt
7777
}
7878
};
7979

80+
// TODO(c0bra): Turn this into a deep array watch
8081
var colWatchDereg = $scope.$watch( 'col', cellChangeFunction );
8182
var rowWatchDereg = $scope.$watch( 'row', cellChangeFunction );
8283

src/js/core/directives/ui-grid-row.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,34 @@
2626
// Function for attaching the template to this scope
2727
var clonedElement, cloneScope;
2828
function compileTemplate() {
29-
var compiledElementFn = $scope.row.compiledElementFn;
29+
$scope.row.getRowTemplateFn.then(function (compiledElementFn) {
30+
// var compiledElementFn = $scope.row.compiledElementFn;
3031

31-
// Create a new scope for the contents of this row, so we can destroy it later if need be
32-
var newScope = $scope.$new();
32+
// Create a new scope for the contents of this row, so we can destroy it later if need be
33+
var newScope = $scope.$new();
3334

34-
compiledElementFn(newScope, function (newElm, scope) {
35-
// If we already have a cloned element, we need to remove it and destroy its scope
36-
if (clonedElement) {
37-
clonedElement.remove();
38-
cloneScope.$destroy();
39-
}
35+
compiledElementFn(newScope, function (newElm, scope) {
36+
// If we already have a cloned element, we need to remove it and destroy its scope
37+
if (clonedElement) {
38+
clonedElement.remove();
39+
cloneScope.$destroy();
40+
}
4041

41-
// Empty the row and append the new element
42-
$elm.empty().append(newElm);
42+
// Empty the row and append the new element
43+
$elm.empty().append(newElm);
4344

44-
// Save the new cloned element and scope
45-
clonedElement = newElm;
46-
cloneScope = newScope;
45+
// Save the new cloned element and scope
46+
clonedElement = newElm;
47+
cloneScope = newScope;
48+
});
4749
});
4850
}
4951

5052
// Initially attach the compiled template to this scope
5153
compileTemplate();
5254

5355
// If the row's compiled element function changes, we need to replace this element's contents with the new compiled template
54-
$scope.$watch('row.compiledElementFn', function (newFunc, oldFunc) {
56+
$scope.$watch('row.getRowTemplateFn', function (newFunc, oldFunc) {
5557
if (newFunc !== oldFunc) {
5658
compileTemplate();
5759
}

src/js/core/services/gridClassFactory.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,6 @@
194194

195195
// Use the grid's function for fetching the compiled row template function
196196
row.getRowTemplateFn = grid.getRowTemplateFn;
197-
198-
// Get the compiled row template function...
199-
grid.getRowTemplateFn.then(function (rowTemplateFn) {
200-
// And assign it to the row
201-
row.compiledElementFn = rowTemplateFn;
202-
});
203197
}
204198
// Row has its own template assigned
205199
else {
@@ -212,10 +206,7 @@
212206
.then(function (template) {
213207
// Compile the template
214208
var rowTemplateFn = $compile(template);
215-
216-
// Assign the compiled template function to this row
217-
row.compiledElementFn = rowTemplateFn;
218-
209+
219210
// Resolve the compiled template function promise
220211
perRowTemplateFnPromise.resolve(rowTemplateFn);
221212
},
@@ -224,6 +215,8 @@
224215
throw new Error("Couldn't fetch/use row template '" + row.rowTemplate + "'");
225216
});
226217
}
218+
219+
return row.getRowTemplateFn;
227220
}
228221
};
229222

test/unit/core/directives/ui-grid-row.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ddescribe('uiGridRow', function () {
1+
describe('uiGridRow', function () {
22
var grid, data, columnDefs, $scope, $compile, $document, recompile, uiGridConstants, GridRow, gridUtil;
33

44
data = [
@@ -43,7 +43,7 @@ ddescribe('uiGridRow', function () {
4343
}));
4444

4545
describe('with different row templates', function () {
46-
beforeEach(inject(function($templateCache) {
46+
beforeEach(inject(function($templateCache, $q) {
4747
$templateCache.put('customRowTemplate', '<div><div>The name is: {{ row.entity.name }}</div></div>');
4848

4949
$scope.gridApi.grid.registerRowsProcessor(function alterTemplates(rows, cols) {
@@ -54,7 +54,7 @@ ddescribe('uiGridRow', function () {
5454
row.rowTemplate = 'customRowTemplate';
5555
gridUtil.getTemplate(row.rowTemplate)
5656
.then(function (template) {
57-
row.compiledElementFn = $compile(template);
57+
row.getRowTemplateFn = $q.when($compile(template));
5858
});
5959
}
6060
});

0 commit comments

Comments
 (0)