Nickon
Nickon

Reputation: 10146

Using custom JQuery scripts in Zend 2

I'm new in Zend Framework and I try to deal with some JS in my project. I have the following code in my layout.phtml:

<head>
    <!-- Scripts -->
    <?php echo $this->headScript()->prependFile($this->basePath() . '/js/html5.js', 'text/javascript', array('conditional' => 'lt IE 9',))
                                  ->prependFile($this->basePath() . '/js/jquery.min.js')
                                  ->prependFile($this->basePath() . '/js/bonsai.min.js')
                                  ->prependFile($this->basePath() . '/js/ui-controller.js'); ?>

</head>

My ui-controller.js contains simple JQuery script:

$(document).ready(function() {
    alert('test);
});

The problem is it isn't executed at all... I checked in Firebug in Chrome and my file is linked correctly. HTML code seems to be okay aswell... Can anyone help me?

[edit]
I wanted to say that I know I can use ZendX_JQuery plugin but I wonder how to use other JavaScript libraries when this one doesn't work...

[edit 2]

I get this error in the console:

Uncaught ReferenceError: $ is not defined ui-controller.js:1 GET
http://myproject.localhost/js/jquery-1.10.2.min.map 404 (Not Found)

Upvotes: 0

Views: 862

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

I'm guessing the order of your JS files is the issue. Because you are successively calling prependFile(), the ui-controller.js file will end up first in the HTML source, so it's loading before jQuery. Switch these calls to appendFile() instead (or reverse their order) and it should work fine.

Upvotes: 1

Related Questions