Return Value of Function as items Pattern

This is an archived post from the ExtJS era — the code may be obsolete, but the principles often still apply.

Code in this post can be obsolete, however, principles and theory may still apply.

6 comments

  1. LucianMay 26, 2008archive
    A good example on how to use this pattern: <pre> { 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 &lt;= dt.format('Y'); i++){ years.push(i); } return years; }).createDelegate(this)() } </pre> That code will render a combobox for selecting one year between 1900 and the current year, basically an "year selector".
  2. SakiMay 26, 2008archive
    @Lucian Exactly! I see that you grasped it fully. You can use same pattern also for <i>items</i> array if you need to.
  3. zeruyoAugust 26, 2009archive
    I don't understand what the difference is with items:function() { var items[]; . . . return items; }()
  4. thezverMarch 3, 2010archive
    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 .
  5. plop plopOctober 24, 2014archive
    I'm trying to use this method to attache dynamicly a parameter to a proxy in a model. proxy : { extraParams : { test : (function(){ return 'test'; }).createDelegate(this)() }, .... This cause an error : Uncaught TypeError: undefined is not a function Where am I wrong ?
    1. SakiNovember 5, 2014archive
      createDelegate? This function has been removed in Ext 4. Now you would use something like this: <pre> Ext.Function.bind(function(){alert('Hello World!')}, this)() </pre>

Comments are closed on archived posts.