Return Value of Function as items Pattern

2. April 2008 – 17:37
,items:(function(){
    var items = [];
 
    // populate items array
 
    return items;
}).createDelegate(this)()

StumbleUpon Toolbar
  1. 4 Responses to “Return Value of Function as items Pattern”

  2. A good example on how to use this pattern:

    {
         xtype: 'combo'
        ,fieldLabel: 'Combobox field label'
        ,width:186
        ,typeAhead: true
        ,displayField: 'value'
        ,mode: 'local'
        ,value: new Date().format('Y')
        ,triggerAction: 'all'
        ,hideOnSelect:false
        ,emptyText:'Select year...'
        ,store: (function(){
            var years = [];         
            var dt = new Date();
            for(var i=1900; i <= dt.format('Y'); i++){
                years.push(i);
            }    
            return years;
        }).createDelegate(this)()
    }

    That code will render a combobox for selecting one year between 1900 and the current year, basically an “year selector”.

    By Lucian on May 26, 2008

  3. @Lucian

    Exactly! I see that you grasped it fully. You can use same pattern also for items array if you need to.

    By Saki on May 26, 2008

  4. I don\’t understand what the difference is with

    items:function()
    {
    var items[];
    .
    .
    .
    return items;
    }()

    By zeruyo on Aug 26, 2009

  5. the difference is scope : in case you need to refer to properties/methods of the class itself (`this`) inside the function.
    also, i think it\’s a good practice to NOT use inline functions (closures) this way, but to refer to defined methods of the class, or to \’helper\’ methods.
    correct me if i\’m wrong .

    By thezver on Mar 3, 2010

Post a Comment

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a