Declined
Last Updated: 25 Nov 2021 11:52 by ADMIN
Il
Created on: 08 Jun 2016 09:06
Category: Data Source
Type: Feature Request
1
Allow all selectors (instead of only by ID) in the data-template options
Currently all "data*-template" options are limited to use ad #ID selector to pick the template script from the page.
This is problematic in big SPAs that have multiple pages dynamically loaded with possible collisions in IDs.
To avoid the hassle of mantaining unique ids across all pages (or having to dynamically generate ids) I therefore propose to allow for any jQuery selector in those options.

<script type="text/template" class="myAwesomeTemplate"></script>
<div data-bind="source: dataSource" data-template=".myAwesomeTemplate"></div>

To maintain retrocompatibility a first search with the "#" can be performed before executing the selector.

Since I am not very good with GitHub, here is the code that I did implement:

function parseOptions(element, options) {
    var result = {}, option, value, templateElement;
    for (option in options) {
        value = parseOption(element, option);
        if (value !== undefined) {
            if (templateRegExp.test(option)) {
                templateElement = null;
                if(!value) {
                    throw new Error("Missing template selector in option " + option);
                }
                try {
                    templateElement = $("#" + value);
                    if(!templateElement.length) {
                        templateElement = $(value).first();
                    }
                } catch (jQueryError) {
                    throw new Error("Invalid template selector " + value + " for option " + option);
                }
                if(!templateElement.length) {
                    throw new Error("Can't find a template with the specified " + value + " selector in option " + option);
                }
                value = kendo.template(templateElement.html());
            }
            result[option] = value;
        }
    }
    return result;
}
0 comments