tags:

views:

43

answers:

3

Hi,

I am using the JS helper in Cake 1.3 and due to the need to use jQuery in noConflict mode, I have to set this in every view:

$this->Js->JqueryEngine->jQueryObject = 'jQuery';

I have a LOT of views that rely on this, and I'd like to avoid having to enter this line at the top of every view that needs it. I tried setting the jQueryObject var in my app_controller.php file, but it did not work. I'd rather not hack the core jquery_engine.php file.

Is there a way to set the jQueryObject var globally from within my app?

Thanks!

+1  A: 

There's probably no way to set the default value "externally" without breaking MVC constraints. You can simply subclass the JsHelper and customize it internally though:

/app/views/helpers/my_js.php

App::import('Helper', 'Js');

class MyJsHelper extends JsHelper {

    public function __construct($settings = array()) {
        parent::construct($settings);
        $this->JqueryEngine->jQueryObject = 'jQuery';
    }

}

This does mean you have to change every instance of $this->Js to $this->MyJs, but shouldn't otherwise be a problem.

(Untested solution, since I've never touched the JsHelper, but you get the idea...)


PS: You may also be able to simply subclass the JqueryEngineHelper directly, overriding the var $jQueryObject = '$'; with var $jQueryObject = 'jQuery';. Again though, as I've never touched the JsHelper, I don't know if it would cause any problems to rename the class (as you will have to when subclassing).

deceze
Thanks for the reply, this led me to a better idea :: see my solution below.
saturn_rising
@saturn Not really a *better* idea, it's the idea I wrote in my PS... :) Glad it worked, good to know.
deceze
@deceze Yes indeed you did! I swear I came up with it independently, which just goes to show great minds think alike. Thanks again.
saturn_rising
@saturn Indeed, LOL. :D
deceze
A: 

Why not do this in your layout? That should propgate down to all your views. Just make sure the setting would be below

print $scripts_for_layout;

so that jquery.js would be loaded.

Nigel
I did think of this, but what I forgot to mention is that my views are being called via ajax. The Js class properties need to be set before the Js buffer is written, which in my case happens before the layout is included, so adding the statement to the layout file (default or ajax) doesn't work.
saturn_rising
+1  A: 

How I solved it:

I created my own Js Engine helper (views/helpers/my_jquery_engine.php) with the following code:

App::import('Helper', 'JqueryEngine');

class MyJqueryEngineHelper extends JqueryEngineHelper {
    var $jQueryObject = 'jQuery';

}

Then in my app_controller, I say: var $helpers = array('Js' => array('MyJquery')); Works like a charm.

saturn_rising