|
1 | 1 | /// <reference path="footer.js" /> |
2 | 2 | /// <reference path="../services/SortService.js" /> |
3 | 3 | /// <reference path="../../lib/jquery-1.8.2.min" /> |
4 | | -var ngGrid = function ($scope, options, sortService, domUtilityService, $filter, $templateCache, $utils, $timeout, $parse) { |
| 4 | +var ngGrid = function ($scope, options, sortService, domUtilityService, $filter, $templateCache, $utils, $timeout, $parse, $http, $q) { |
5 | 5 | var defaults = { |
6 | 6 | //Define an aggregate template to customize the rows when grouped. See github wiki for more details. |
7 | 7 | aggregateTemplate: undefined, |
@@ -215,32 +215,46 @@ var ngGrid = function ($scope, options, sortService, domUtilityService, $filter, |
215 | 215 | self.data = []; |
216 | 216 | self.lateBindColumns = false; |
217 | 217 | self.filteredRows = []; |
| 218 | + |
| 219 | + self.initTemplates = function() { |
| 220 | + var templates = ['rowTemplate', 'aggregateTemplate', 'headerRowTemplate', 'checkboxCellTemplate', 'checkboxHeaderTemplate', 'menuTemplate', 'footerTemplate']; |
| 221 | + |
| 222 | + var promises = []; |
| 223 | + templates.forEach(function(template) { |
| 224 | + promises.push( self.getTemplate(template) ); |
| 225 | + }); |
| 226 | + |
| 227 | + return $q.all(promises); |
| 228 | + }; |
218 | 229 |
|
219 | 230 | //Templates |
220 | 231 | // test templates for urls and get the tempaltes via synchronous ajax calls |
221 | | - var getTemplate = function (key) { |
| 232 | + self.getTemplate = function (key) { |
222 | 233 | var t = self.config[key]; |
223 | 234 | var uKey = self.gridId + key + ".html"; |
| 235 | + var p = $q.defer(); |
224 | 236 | if (t && !TEMPLATE_REGEXP.test(t)) { |
225 | | - $templateCache.put(uKey, $.ajax({ |
226 | | - type: "GET", |
227 | | - url: t, |
228 | | - async: false |
229 | | - }).responseText); |
| 237 | + $http.get(t, { |
| 238 | + cache: $templateCache |
| 239 | + }) |
| 240 | + .success(function(data){ |
| 241 | + $templateCache.put(uKey, data); |
| 242 | + p.resolve(); |
| 243 | + }) |
| 244 | + .error(function(err){ |
| 245 | + p.reject("Could not load template: " + t); |
| 246 | + }); |
230 | 247 | } else if (t) { |
231 | 248 | $templateCache.put(uKey, t); |
| 249 | + p.resolve(); |
232 | 250 | } else { |
233 | 251 | var dKey = key + ".html"; |
234 | 252 | $templateCache.put(uKey, $templateCache.get(dKey)); |
| 253 | + p.resolve(); |
235 | 254 | } |
| 255 | + |
| 256 | + return p.promise; |
236 | 257 | }; |
237 | | - getTemplate('rowTemplate'); |
238 | | - getTemplate('aggregateTemplate'); |
239 | | - getTemplate('headerRowTemplate'); |
240 | | - getTemplate('checkboxCellTemplate'); |
241 | | - getTemplate('checkboxHeaderTemplate'); |
242 | | - getTemplate('menuTemplate'); |
243 | | - getTemplate('footerTemplate'); |
244 | 258 |
|
245 | 259 | if (typeof self.config.data == "object") { |
246 | 260 | self.data = self.config.data; // we cannot watch for updates if you don't pass the string name |
@@ -442,35 +456,41 @@ var ngGrid = function ($scope, options, sortService, domUtilityService, $filter, |
442 | 456 | } |
443 | 457 | }; |
444 | 458 | self.init = function() { |
445 | | - //factories and services |
446 | | - $scope.selectionProvider = new ngSelectionProvider(self, $scope, $parse); |
447 | | - $scope.domAccessProvider = new ngDomAccessProvider(self); |
448 | | - self.rowFactory = new ngRowFactory(self, $scope, domUtilityService, $templateCache, $utils); |
449 | | - self.searchProvider = new ngSearchProvider($scope, self, $filter); |
450 | | - self.styleProvider = new ngStyleProvider($scope, self); |
451 | | - $scope.$watch('configGroups', function(a) { |
452 | | - var tempArr = []; |
453 | | - angular.forEach(a, function(item) { |
454 | | - tempArr.push(item.field || item); |
455 | | - }); |
456 | | - self.config.groups = tempArr; |
457 | | - self.rowFactory.filteredRowsChanged(); |
458 | | - $scope.$emit('ngGridEventGroups', a); |
459 | | - }, true); |
460 | | - $scope.$watch('columns', function (a) { |
461 | | - domUtilityService.BuildStyles($scope, self, true); |
462 | | - $scope.$emit('ngGridEventColumns', a); |
463 | | - }, true); |
464 | | - $scope.$watch(function() { |
465 | | - return options.i18n; |
466 | | - }, function(newLang) { |
467 | | - $utils.seti18n($scope, newLang); |
| 459 | + return self.initTemplates().then(function(){ |
| 460 | + //factories and services |
| 461 | + $scope.selectionProvider = new ngSelectionProvider(self, $scope, $parse); |
| 462 | + $scope.domAccessProvider = new ngDomAccessProvider(self); |
| 463 | + self.rowFactory = new ngRowFactory(self, $scope, domUtilityService, $templateCache, $utils); |
| 464 | + self.searchProvider = new ngSearchProvider($scope, self, $filter); |
| 465 | + self.styleProvider = new ngStyleProvider($scope, self); |
| 466 | + $scope.$watch('configGroups', function(a) { |
| 467 | + var tempArr = []; |
| 468 | + angular.forEach(a, function(item) { |
| 469 | + tempArr.push(item.field || item); |
| 470 | + }); |
| 471 | + self.config.groups = tempArr; |
| 472 | + self.rowFactory.filteredRowsChanged(); |
| 473 | + $scope.$emit('ngGridEventGroups', a); |
| 474 | + }, true); |
| 475 | + $scope.$watch('columns', function (a) { |
| 476 | + domUtilityService.BuildStyles($scope, self, true); |
| 477 | + $scope.$emit('ngGridEventColumns', a); |
| 478 | + }, true); |
| 479 | + $scope.$watch(function() { |
| 480 | + return options.i18n; |
| 481 | + }, function(newLang) { |
| 482 | + $utils.seti18n($scope, newLang); |
| 483 | + }); |
| 484 | + self.maxCanvasHt = self.calcMaxCanvasHeight(); |
| 485 | + if (self.config.sortInfo.fields && self.config.sortInfo.fields.length > 0) { |
| 486 | + self.getColsFromFields(); |
| 487 | + self.sortActual(); |
| 488 | + } |
468 | 489 | }); |
469 | | - self.maxCanvasHt = self.calcMaxCanvasHeight(); |
470 | | - if (self.config.sortInfo.fields && self.config.sortInfo.fields.length > 0) { |
471 | | - self.getColsFromFields(); |
472 | | - self.sortActual(); |
473 | | - } |
| 490 | + |
| 491 | + // var p = $q.defer(); |
| 492 | + // p.resolve(); |
| 493 | + // return p.promise; |
474 | 494 | }; |
475 | 495 |
|
476 | 496 | self.resizeOnData = function(col) { |
@@ -565,7 +585,7 @@ var ngGrid = function ($scope, options, sortService, domUtilityService, $filter, |
565 | 585 | angular.forEach(tempData, function(item, i) { |
566 | 586 | var e = self.rowMap[i]; |
567 | 587 | if (e != undefined) { |
568 | | - var v = self.rowCache[v]; |
| 588 | + var v = self.rowCache[i]; |
569 | 589 | if(v != undefined) { |
570 | 590 | item.preSortSelected = v.selected; |
571 | 591 | item.preSortIndex = i; |
@@ -813,6 +833,4 @@ var ngGrid = function ($scope, options, sortService, domUtilityService, $filter, |
813 | 833 | } |
814 | 834 | return newDim; |
815 | 835 | }; |
816 | | - //call init |
817 | | - self.init(); |
818 | 836 | }; |
0 commit comments