Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(getTemplate): Updated custom templates as promises condition #5311

Merged
merged 1 commit into from May 27, 2016
Merged

Conversation

Edifear
Copy link
Contributor

@Edifear Edifear commented Apr 12, 2016

now custom promise templates will handled properly, e.g.:
headerCellTemplate: $http.get('./template.html').then(function(res) {return res.data;})

fixes issue #4514

@david-hanger
Copy link

david-hanger commented Aug 22, 2017

The referenced fix for issues #4514 and #5717 works for the above example (explicitly assigning a promise), but it is not quite a complete fix. You are correct that headerCellTemplate now handles a promise value, but it still does not correctly handle a URL as the value. The problem is that the uiGridHeaderCell.compile.pre presumes that $scope.col.headerCellTemplate is already set, but gridClassFactory.defaultColumnBuilder leaves col[templateType] undefined until after resolving the GET request on the URL. The reason the above example works is because headerCellTemplate is immediately assigned the promise as a value. If you change it to this:
headerCellTemplate: './template.html'
... then the headerCellTemplate is treated as empty because the underlying col value is left undefined when read (unless it happens to get assigned before being read, which is unlikely).

Please consider updating this fix to include something akin to the following fix I made for the same issue with filterHeaderTemplate (which can trivially be applied to headerCellTemplate and any other templates whose value is read without dealing with promises). I'm basing this off the current master, commit 84ea8fa.
(sorry; my fixes are on a network without internet access, so I can't provide a proper diff; I'm also attempting to format similarly to the adjacent code).

src/js/core/services/gridClassFactory.js

replace line 101:

             templateGetPromises.push(gridUtil.getTemplate(col[providedType])

... with:

             var promise;
             // reuse existing promise
             if ( col[templateType] && angular.isFunction(col[templateType].then) ){
                 promise = col[templateType];
             }
             // likewise if there is a related promise, e.g. cellTemplatePromise
             if ( col[templateType + 'Promise'] ){
                 promise = col[templateType + 'Promise'];
             }
             else {
                 promise = gridUtil.getTemplate(col[providedType])

Add this after line 118 (following the } after col[templateType] = template;):

                   // resolve the final value for this promise chain
                   return col[templateType];

replace lines 122-123:

                }).catch(angular.noop)
            );

... with:

                }).catch(angular.noop);
             }
             // add the promise to the list (either new or reused prior promise)
             templateGetPromises.push(promise);
             // assign the promise as an intermediate template value, until resolved
             if ( !col[templateType] ){
               col[templateType] = promise;
             }

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

replace line 4:

  angular.module('ui.grid').directive('uiGridFilter', ['$compile', '$templateCache', 'i18nService', 'gridUtil', function ($compile, $templateCache, i18nService, gridUtil) {

... with (adding $q):

  angular.module('ui.grid').directive('uiGridFilter', ['$compile', '$templateCache', '$q', 'i18nService', 'gridUtil', function ($compile, $templateCache, $q, i18nService, gridUtil) {

replace lines 13-15:

                var template = $scope.col.filterHeaderTemplate;

                $elm.append($compile(template)($scope));

... with:

                // handle promise values
                $q.when( $scope.col.filterHeaderTemplate ).then( function( template ){
                  $elm.append($compile(template)($scope));
                });

Note that this also fixes a minor bug where a URL in the cellTemplate gets fetched multiples times (twice, in my observation) because it isn't added to the $templateCache until after the GET response has been handled. The above fix reuses pending promises to avoid these duplicate fetches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants