Frage

I have been playing around with the kendo-mobile demos and I am a bit stumped on one of the examples. In this particular example, there is a piece of javascript

 <script id="hierarchicalMobileListViewTemplate" type="text/x-kendo-template">
    # if (data.HasEmployees) { #
            <h2> #: FullName # </h2>
        </a>
    # } else { #
        <h2> #: FullName # </h2>
    # } #
</script>

which has the data.HasEmployees, and I'm unsure where the "data" part comes from as it isn't a variable anywhere in code.

Here is the example http://trykendoui.telerik.com/ufup. Any help would be greatly appreciated.

Thanks

Michael

p.s code copied in its entirety

<!DOCTYPE html>
<html>
<head>
    <base href="http://demos.telerik.com/kendo-ui/mobile/listview/hierarchical-databinding.html">
    <style>html { font-size: 12px; font-family: Arial, Helvetica, sans-serif; }</style>
    <title></title>
    <link href="http://cdn.kendostatic.com/2014.1.416/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="http://cdn.kendostatic.com/2014.1.416/styles/kendo.default.min.css" rel="stylesheet" />
    <link href="http://cdn.kendostatic.com/2014.1.416/styles/kendo.mobile.all.min.css" rel="stylesheet" />
    <script src="http://cdn.kendostatic.com/2014.1.416/js/jquery.min.js"></script>
    <script src="http://cdn.kendostatic.com/2014.1.416/js/kendo.all.min.js"></script>
</head>
<body>
    <div data-role="view" data-show="rebindListView" id="hierarchical-view" data-transition="slide">
    <header data-role="header">
        <div data-role="navbar" id="employee-navbar">
            <a data-role="backbutton" id="employee-back" data-align="left">Back</a>
            <span data-role="view-title"></span>
            <a data-align="right" data-role="button" class="nav-button" href="#/">Index</a>
        </div>
    </header>
    <ul id="hierarchical-listview" data-role="listview" data-template="hierarchicalMobileListViewTemplate"></ul>
</div>


 <script id="hierarchicalMobileListViewTemplate" type="text/x-kendo-template">
    # if (data.HasEmployees) { #
            <h2> #: FullName # </h2>
        </a>
    # } else { #
        <h2> #: FullName # </h2>
    # } #
</script>

<script>


    var serviceRoot = "http://demos.telerik.com/kendo-ui/service";

    var employees = new kendo.data.HierarchicalDataSource({
        transport: {
            read: {
                url: serviceRoot + "/Employees",
                dataType: "jsonp"
            }
        },

        schema: {
            model: {
                id: "EmployeeId",
                hasChildren: "HasEmployees"
            }
        }
    });

    function rebindListView(e) {
        var parentID = e.view.params.parent,
            element = e.view.element,
            backButton = element.find('#employee-back'),
            listView = element.find("#hierarchical-listview").data('kendoMobileListView'),
            navBar = element.find('#employee-navbar').data('kendoMobileNavBar');

        if (parentID) {
            employees.fetch(function() {
                var item = employees.get(parentID);
                if (item) {
                    backButton.show();
                    navBar.title(item.FullName);
                    listView.setDataSource(item.children);
                } else {
                    // redirect to root
                    setTimeout(function() {
                        kendo.mobile.application.navigate('#hierarchical-view');
                    }, 0);
                }
            });
        } else {
            backButton.hide();
            navBar.title('Employees');
            listView.setDataSource(employees);
        }

        e.view.scroller.scrollTo(0, 0);
    }
</script>


<script>
    var app = new kendo.mobile.Application(document.body);
</script>
</body>
</html>
War es hilfreich?

Lösung

The data variable refers to the observable which is passed to the template (a data item in the data source, in your example). Kendo UI uses a with block for templates; this means that the observable is added to the scope chain the template variables are evaluated in; so you could also use this instead:

 <script id="hierarchicalMobileListViewTemplate" type="text/x-kendo-template">
    # if (HasEmployees) { #
        <h1> #: FullName # </h1>
    # } else { #
        <h2> #: data.FullName # </h2>
    # } #
</script>

You might also want to read this answer.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top