Use .on () instead of .bind () in CakePHP's JsHelper

7

When I write Ajax with CakePHP's JsHelper, if I write something like this

$this->Js->get('#searchCity')->event(
        'click', $this->Js->request(
                'http://api.geonames.org/searchJSON', array(
            'async' => true,
            'dataExpression' => true,
            'method' => 'GET',
            'data' => "{}",
            'dataType' => 'JSON',
            'success' => ""
                )
        )
);

the output is something like this

$(document).ready(function() {
    $("#searchCity").bind("click", function(event) {
        $.ajax({async: true, data: {},
            dataType: "JSON", success: function(data, textStatus) {
            }, type: "GET", url: "http:\/\/api.geonames.org\/searchJSON"});
        return false;
    });
});

Apparently, the .bind () function does not work in Internet Explorer, and I read that I can use .on () to replace it.

The question is: can I change these methods without having to mess with CakePHP's core or is there any smarter solution?

    
asked by anonymous 14.02.2014 / 13:31

3 answers

2

Looking at the I believe you will need create your custom helper.

Basically you will copy all dependencies from the and do a change in line 183 of file JqueryEngigeHelper.php of:

return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback);

To:

return sprintf('%s.on("%s", %s);', $this->selection, $type, $callback);

To find the files you need to duplicate, go to the folder:

lib/Cake/View/Helper/JqueryEngineHelper.php
    
14.02.2014 / 13:53
0

It does not have an apparent reason for not working on IE looks at the internal bind code in jquery

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
}

Now calling on

on: function(types, selector, data, fn)

Then, as we can see the bind calls the on passing the second parameter that would be the selector as null , only that.

    
14.02.2014 / 21:17
-1

The on () method is the bind () , you can change without fear.

of the documentation:

  

As of jQuery 1.7, the .on () method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind () method is used for attaching an event handler directly to elements.

    
14.02.2014 / 13:37