[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] brooklyn-ui pull request #113: Fix template overrides - decorate `script` di...

Github user ahgittin commented on a diff in the pull request:

    --- Diff: ui-modules/utils/script-tag-non-overwrite/script-tag-non-overwrite.js ---
    @@ -0,0 +1,48 @@
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +import angular from 'angular';
    + * If included, this decorates the default angular `<script>` tag so that it checks the
    + * template cache and does _not_ put the contents of the `script` into the cache if there
    + * is already an element with that ID present.
    + */
    +const MODULE_NAME = 'brooklyn.components.script-tag-non-overwrite';
    +angular.module(MODULE_NAME, [])
    +    .decorator('scriptDirective', ['$delegate', '$templateCache', scriptTagDirectiveDecorator]);
    +export default MODULE_NAME;
    +const BROOKLYN_CONFIG = 'brooklyn.config';
    +function scriptTagDirectiveDecorator($delegate, $templateCache) {
    +    let base = $delegate[0];
    +    return [ Object.assign({}, base, { compile: function(el, attr) {
    +        let match = $templateCache.get(attr.id);
    +        if (!(match === null || typeof match === 'undefined')) {
    --- End diff --
    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 .