Use . on() instead of . bind() in Cakephp’s Jshelper

Asked

Viewed 162 times

7

When I write Ajax with Cakephp’s Jshelper, 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 more or less 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 tamper with Cakephp’s core or is there some smarter solution?

  • What version of your cakephp?

  • tried "onclick" instead of "click"?

  • I think I figured out what’s going on, and it has nothing to do with . bind(). I’m trying to request an external url, and IE is blocking me because of the Same origin policy. Is there any way around this??

  • Yes, please ask another question, with Ajax Cross Domain. Just to keep the site "organized," choose an answer like the one you think is right.

3 answers

2

Observing the native code of I believe you will need to create your custom helper.

Basically you will copy all the dependencies of and make a change to line 183 of the archive 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 will need to duplicate access the folder:

lib/Cake/View/Helper/JqueryEngineHelper.php

0

No apparent reason not to work in IE look at the internal bind code in jquery

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

Now the call of on

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

So, how do we notice the bind calls the on passing the second parameter that would be the selector as null, only that.

  • The answer does not answer the question.

-1

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

of 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.

  • I know I can change, but the problem is: How to tell Helper to use . on() instead of . bind()???

Browser other questions tagged

You are not signed in. Login or sign up in order to post.