Search code examples
javascriptajaxkendo-uimustachekendo-mobile

how to bind a data-click to a div populated in a template with kendo UI?


I'm trying to populate a template with kendo UI where I attach a data-click to some <button>.

The template is populated after an ajax request (when().done()) and the data-click doesn't seem to work. Why ? how can I fix this ?

Thanks

Code: (I'm using kendo ui and Moustache) html

    <!-- Templatr for Event Lists -->
    <script id="listTemplate" type="text/x-handlebars-template">

        {{#each this}}
            <div class="post">
                <div class="post-content" data-index="{{@index}}" data-id="{{id}}">
                    <div class="post-header"> 
                        <span class="outing-name">{{name}}</span> - {{type}}
                        <br /> at <span class="outing-category">{{place.name}}</span> ( {{dist}} km)
                    </div>
                    <div class="post-image">
                        <img src="{{pic}}">
                    </div>
                    <div class="post-desc">
                        {{{short_desc}}} [...]
                    </div>
                </div>

                <div class="post-actions">
                    <a data-role="button" data-click="notifyEvent" class="buttonNotify" data-id="{{id}}" data-name="{{name}}" data-type="{{type}}">I want to go</a>
                    <a href="#" data-role="button">Friends</a>
                    <a href="#" data-role="button" data-id="{{id}}">More details</a>
                </div>
            </div>
        {{/each}}

    </script>

Solution

  • try using data-bind:"click: notifyEvent" and have the function initialized in your ViewModel like this :

     var viewModel = kendo.observable({
    
                    notifyEvent: notifyEvent,
    
                    ... other functions, objects or kendo.data.DataSource...
    
                });
        function notifyEvent{ ..do stuff here... }
    
    
     $.extend(window, {
            viewModel: viewModel 
        });
    

    be sure to have your view initialized something like this:

    <div data-role="view" id="ListView"  data-title="ListView" data-layout="listTemplate"  data-model="viewModel"> 
    

    hope it helps! Cheers!