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 template overrides - decorate script directive to prevent re-overriding of templates #113

Merged
merged 4 commits into from
Nov 25, 2018

Conversation

ahgittin
Copy link
Contributor

by default <script> will install to template cache whenever it is processed as a directive.
this decorates it to be no-op if there is already something in the cache with that name,
allowing programmatic overrides of templates configured using <script id="..."> notation.

not really needed, but is consistent with how done elsewhere,
and allows override
by default `<script>` will install to template cache whenever it is processed as a directive.
this decorates it to be no-op if there is already something in the cache with that name,
allowing programmatic overrides of templates configured using `<script id="...">` notation.
@sferot
Copy link
Contributor

sferot commented Nov 23, 2018

It works fine for me 👍

Copy link
Member

@tbouron tbouron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @ahgittin, just 2 small comments

@@ -79,7 +82,7 @@ export function specEditorDirective($rootScope, $templateCache, $injector, $sani
model: '='
},
controller: ['$scope', '$element', controller],
template: template,
templateUrl: TEMPLATE_URL,
Copy link
Member

@tbouron tbouron Nov 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you do this, could you change it to match what the other directives do?

templateUrl: function (tElement, tAttrs) {
return tAttrs.templateUrl || TEMPLATE_URL;
},

So it can be overridden directly from the attribute template-url on the directive itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot, sorry i missed this!

let base = $delegate[0];
return [ Object.assign({}, base, { compile: function(el, attr) {
let match = $templateCache.get(attr.id);
if (!(match === null || typeof match === 'undefined')) {
Copy link
Member

@tbouron tbouron Nov 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be reluctant to change the default behaviour for the entire application without a possibility to go back. I think it would be wise to add a override (or a better name) parameter to revert to the default behaviour if we need/want to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wondered about this too. we have unique names for all our things and i don't see any way someone would have registered a template that isn't meant to override our things, so didn't think we needed an "opt-out".

however there is a risk with 3rd party libraries that templates might have non-unique names and this could cause an issue there. doesn't seem to be a problem but if we're concerned what i'd suggest is that we make this behaviour "opt-in", checking an extra attribute e.g. defer-to-preexisting-id, which we set everywhere in our code that we define a <script id="..." ... tag.

not sure it's needed but will add if you think so @tbouron .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahgittin Aye, that was the use-case I had in mind. I don't think it's strictly speaking required but better safe than sorry => I think it would be better to do this change.

Although, there is another solution that is even simpler: don't decorate the script directive and update the way we register template to do that in a .config block (like the main template) rather than using <script> tags

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, done @tbouron , see last commit

this allows scripts in templates to "opt-in" to being overridable;
our custom decorator has no effect unless this attribute is set,
so we guarantee not to change any behaviour in 3rd party libraries
Copy link
Member

@tbouron tbouron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @ahgittin. Will merge once Jenkins is happy

@asfgit asfgit merged commit 116c50e into apache:master Nov 25, 2018
asfgit pushed a commit that referenced this pull request Nov 25, 2018
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.

4 participants