This is a migrated thread and some comments may be shown as answers.

Binding to widget events

7 Answers 729 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Samuel PIckard
Top achievements
Rank 1
Samuel PIckard asked on 20 Apr 2012, 10:48 AM
Ok, for the sake of argument, pretend I'm a really thick C++ programmer.

I've made my beautiful UI in HTML5 and KendoUI, but I want to bind a JavaScript function to a checkbox, so when the state of the checkbox changes, my Javascript code is called.  
Currently, I've got this:
<div data-role="view" data-layout="overview-layout" id="tuxedo-home" data-title="Home">   
    <ul data-role="listview" data-style="inset" data-type="group">
        <li>
            <ul>
                <li>Option 1 <input type="checkbox" data-role="switch" checked></li>
                <li>Option 2 <input id="suspendCard" type="checkbox" data-role="switch"></li>
                <li><a href="overview-cities">Advanced</a></li>
            </ul>
        </li>
    </ul>
</div>

And then this:
    <script>
        window.kendoMobileApplication = new kendo.mobile.Application(document.body);
        function notify() { alert("clicked"); }
        $("#suspendCard").on("click", notify);
    </script>

But nothing happens.

7 Answers, 1 is accepted

Sort by
0
Petyo
Telerik team
answered on 20 Apr 2012, 12:28 PM
Hello,

Please refer to the Widget event binding section of our online documentation for the proper way to handle widgets events.

Greetings,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Samuel PIckard
Top achievements
Rank 1
answered on 20 Apr 2012, 03:58 PM
No, you're going to have to imagine I'm even thicker than that.  The document refers to some nice data binding, and the old JQuery bind method.  So I can do this:

$(document).ready(function() {
   $("#tuxedo-home ul li ul li").bind("click", hellomum);
})
And that works well when I click the li my checkbox is in.  But this is my source (from the examples code):

                <li>Option 2 <input id="suspendCard" type="checkbox" data-role="switch" /></li>

I've given the object and id of 'suspendCard' so I *think* I should be able to at least write:
$(document).ready(function() {
    $("#tuxedo-home ul li ul li input").bind("click", hellomum);
 }) 
or better still:
$(document).ready(function() {
    $("#suspendCard").bind("click", hellomum);
 }) 
$(document).ready(function() {
    $("#tuxedo-home ul li ul li").bind("click", hellomum);
 }) 
But neither of these approaches activates the function.
Firebug shows me that my code has been transformed into this:
Option 2
<span class="km-switch km-switch-on">
<input id="suspendCard" type="checkbox" data-role="switch">
<span class="km-switch-wrapper">
<span class="km-switch-background" style="margin-left: -18px; -moz-transition: none 0s ease 0s ;"></span>
</span>
<span class="km-switch-container">
<span class="km-switch-handle" style="-moz-transform: translate(62px, 0px); -moz-transition: none 0s ease 0s ;">
</span>
</span>
</li>
So I guess there are issues about HTML5 not being XML compliant, but also I can't seem to specify correctly the widget I want to be informed about.  If the path isn't "#suspendCard", then what is it?
0
Petyo
Telerik team
answered on 20 Apr 2012, 04:27 PM
Hello,

Our widgets do not expose events through the jQuery event mechanism. This is especially important for the mobile widgets, where touch capabilities are detected and different DOM events are used. Please see this code sample. The bind method is called on the widget instance, not on the jQuery object. 

var autoComplete = $("#products").data("kendoAutoComplete");
autoComplete.bind("change", onChange);
 

Kind regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Samuel PIckard
Top achievements
Rank 1
answered on 20 Apr 2012, 05:23 PM
OK, now we're getting to my level :-)

I've been through the documentation again, and I now think that what I want to do is:

$('#option2').kendoMobileSwitch();

To reference the object.  This is based on this snippet from http://www.kendoui.com/documentation/mobile/switch/overview.aspx 

<input type="checkbox" id="switch" checked="checked" />
<script>
var switchWidget = $("#switch").kendoMobileSwitch();
</script>

So, the problem has now doubled.  Firstly, I still can't attach to the click or change events on the mobile switch.
var s = $("#option2").kendoMobileSwitch();
s.bind("click", hellomum);

Doesn't activate the function.  In fact, the very act of getting the switch has broken the switch.  Just by adding the line

$("#option2").kendoMobileSwitch();

My MobileSwitch no longer renders properly.  See attached screen shots.  This is the code:
<!DOCTYPE html>
<html>
<head>
    <title>Overview</title>
    <script src="js/jquery.min.js"></script>
    <script src="js/kendo.mobile.min.js"></script>
    <script src="content/shared/js/console.js"></script>
    <link href="styles/kendo.common.min.css" rel="stylesheet" />
    <link href="styles/kendo.mobile.all.min.css" rel="stylesheet" />
</head>
<body>
 
<div data-role="view" data-layout="overview-layout" id="tuxedo-home" data-title="Home">  
    <ul data-role="listview" data-style="inset" data-type="group">
        <li>
            <ul>
                <li>Option 1 <input id="option1" type="checkbox" data-role="switch" checked></li>
                <li>Option 2 <input id="option2" type="checkbox" data-role="switch"></li>
                <li><a href="advanced-options">Advanced</a></li>
            </ul>
        </li>
    </ul>
</div>
 
    <script>
       window.kendoMobileApplication = new kendo.mobile.Application(document.body);
    </script>
    <script>
        <!-- Breaks the rendering -->
        $("#option2").kendoMobileSwitch();
    </script>
</body>
</html>




0
Samuel PIckard
Top achievements
Rank 1
answered on 20 Apr 2012, 06:21 PM
OK, I've found the stupid mistake I was making here - .kendoMobileSwitch() creates the Kendo widget from the definition, so I was doing it twice.

Still can't capture the event though when the Switch changes state.
0
Samuel PIckard
Top achievements
Rank 1
answered on 20 Apr 2012, 06:43 PM
Ok, so I've been R'ing TFM - http://www.kendoui.com/documentation/mobile/switch/events.aspx - that's what I was looking for.  I'm beginning to understand how it all hangs together now.

I've now got this:
<input type="checkbox" id="switch" data-role="switch" />
 
<script>
 $("#switch").data("kendoMobileSwitch").bind("change", function(e) {
     //handle change event
 }
</script>
But it still doesn't work!
0
Accepted
Petyo
Telerik team
answered on 21 Apr 2012, 02:11 AM
Hello,

Widget event initialization and event binding needs to be performed in the respective view init event handler (as the widgets are not instantiated immediately). The approach has been discussed in the forums, and is used in most of our online demos. Alternatively, you can use declarative event binding.

I would like to recommend reviewing our online documentation and demos, as well as the example sushi application source code as a reference. 

If you still face troubles or roadblocks, you may open a support ticket with a sample project, so that we can provide the necessary help.
 
All the best,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
General Discussions
Asked by
Samuel PIckard
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Samuel PIckard
Top achievements
Rank 1
Share this question
or