Results 1 to 6 of 6

Thread: passing handler function with json

  1. #1

    Default passing handler function with json

    I load my toolbar via json (different users must see different menus), but I can't tell extjs what the handler for each entry is :

    Code:
    var my_handler = function() {
        alert('TODO');
    }
    
        var north_toolbar = new Ext.Toolbar();
    
        Ext.Ajax.request({
            url:    '/ext/get_north_menu_config',
            success:    function(result, request){
                // for fast debug
                var returned_json = "[{ text: 'alfa', menu: [{ text: 'alfa_one', menu: [{ text: 'alfa_two', handler: 'my_handler' }]}]}]"
                north_toolbar.add(Ext.util.JSON.decode(returned_json));
                north_toolbar.doLayout();
            },
    now, 'my_handler' with quotes won't work, makes sense, tries to use a string as handler ...
    but if I remove the quotes from 'my_handler' the toolbar doesn't render and firebug says :

    my_handler is not defined
    eval()10 (riga 1)
    anonymous()ext-all-debug.js (riga 11527)
    anonymous()ext-all-debug.js (riga 11649)
    anonymous()layout.js (riga 25)
    handleResponse()ext-all-debug.js (riga 8550)
    f()ext-base.js (riga 7)
    anonymous()ext-base.js (riga 7)


    [IMG]chrome://firebug/content/blank.gif[/IMG] return eval("(" + json + ')'); \n

  2. #2
    Sencha User jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    DC Area =)
    Posts
    16,364

    Default

    that's correct. why are you passing a string as a handler?

    Also, no need to call doLayout if the toolbar is not rendered.

  3. #3

    Default

    Right, as a string won't work, what I'm saying is that also :

    Code:
    var returned_json = "[{ text: 'alfa', menu: [{ text: 'alfa_one', menu: [{ text: 'alfa_two', handler: my_handler }]}]}]"
    is not working, with the interpreter complaining :

    my_handler is not defined, even if the function my_handler is defined a few lines before ...

  4. #4

    Default

    The returned_json is a STRING, u can't pass this STRING into extjs as an toolbar OBJECT.

    Actually I don't know what u are going to do, but:

    if you want string:
    var returned_json = "[{ text: 'alfa', menu: [{ text: 'alfa_one', menu: [{ text: 'alfa_two', handler:" + my_handler + "}]}]}]"

    if you want object:
    var returned_json = [{ text: 'alfa', menu: [{ text: 'alfa_one', menu: [{ text: 'alfa_two', handler: my_handler }]}]}]

  5. #5
    Sencha User jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    DC Area =)
    Posts
    16,364

    Default

    I see the problem. my_handler is not within scope of the JSON that is being constructed.

    That said, move my_handler to a global name space such as MyApp.utils.my_handler, this way it can be scoped from anywhere.

  6. #6

    Default

    works, thanks, you saved me from an ictus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •