Block/YUI help please

Block/YUI help please

by Michael Gwynne -
Number of replies: 6

Hi,

I'm trying to create a block with an AJAX call. I have managed this with jQuery before, but now I'm trying with YUI. I think I've managed to include the file correctly, but I'm doing an alert in the init function but nothing is being alerted.

My PHP:

$jsmodule = array(
            'name' => 'block_foobar',
            'fullpath' => '/blocks/foobar_plugin/module.js',
            'requires' => array('base', 'node', 'json')
        );
        
        $this->page->requires->js_init_call('M.block_foobar.init',
                                            NULL,
                                            false,
                                            $jsmodule);

My Javascript:

M.block_foobar={

    Y : null,

    init : function(Y){
        alert('hi');
        this.Y = Y;
     }

};

Also, there are no javascript errors in the firebug window.

Thanks in advance.

Michael

Average of ratings: -
In reply to Michael Gwynne

Re: Block/YUI help please

by Hittesh Ahuja -

This is the equivalent of ajax() in Jquery

 

http://yuilibrary.com/yui/docs/io/

In reply to Hittesh Ahuja

Re: Block/YUI help please

by Hittesh Ahuja -

Hi Michael,

I have tried the same code you pasted and the alert function works just fine for me.

Make sure you are purging your cache everytime you amend the JS file

 

Regards,

Hittesh

Average of ratings: Useful (2)
In reply to Hittesh Ahuja

Re: Block/YUI help please

by Michael Gwynne -

I spent 2 hours trying to figure this out....

I can't believe I didn't make sure the cache was clear.

Thanks

In reply to Michael Gwynne

Re: Block/YUI help please

by john saylor -
haha- we've all been there. but look on the bright side, the next time you won't need the two hours to figure it out! experience is a very inefficient teacher, but sometimes extremely effective.
In reply to Michael Gwynne

Re: Block/YUI help please

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Michael,

I think that what you're doing *should* be working, but I guess something isn't quite right, most likely with the way that you're calling js_init_call. I would imagine that the reason you're not seeing any errors in the firebug is that it's not being included for some reason. Are you seeing any references to M.block_foobar in the page source? You shoudl at least see a call to your init function. If you're not, then I would imagine that you're somehow not calling the js_init_call at all.

You don't say what version of Moodle you're developing for, but I'd really recommend reading the latest version of the JS documentation, specifically the YUI Module documentation at http://docs.moodle.org/dev/YUI/Modules. Although it's still a work in progress (sorry - I will get around to finishing them soon) they should give you a point in the right direction to how we're pushing JS in Moodle now.

Best wishes,

Andrew

In reply to Andrew Lyons

Re: Block/YUI help please

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Oh, and in your config.php, set $CFG->jsrev = -1; to ensure that you aren't caching within Moodle, and disable your browser cache in the browser's developer tools.