1. #1
    Sencha User ksumarine's Avatar
    Join Date
    Feb 2012
    Posts
    16
    Vote Rating
    0
    ksumarine is on a distinguished road

      0  

    Default Answered: XTemplate shared functions

    Answered: XTemplate shared functions


    Hello,

    I am creating an XTemplate inside my list items and I would like to use a common function for all the other XTemplates. For example, this is how it is shown to be used. Very redundant. I would like to declare returnName() once and use it in both XTemplates.

    Code:
    items: [
        {
            title: 'test 1',
            itemTpl: new Ext.XTemplate(
                '<tpl for=".">',
                    '{[this.returnName(values.name)]}',
                '</tpl>',
             {
                  returnName: function(t) {
                      return t;
                  }
             })
        },
        {
            title: 'test 2',
            itemTpl: new Ext.XTemplate(
                '<tpl for=".">',
                    '{[this.returnName(values.name)]}',
                '</tpl>',
             {
                  returnName: function(t) {
                      return t;
                  }
             })
        }
     ]
    Sorry if this has been posted elsewhere, I tried searching and didn't see anything.

    Thanks!

  2. What you need to do is really strip out the logic into a sharable class. These classes are called singleton classes. You can create one like:

    Code:
    Ext.define('MyApp.util.Shared', {
        singleton : true,
    
        myFunction : function(name) {
            return 'something ' + name;
        }
    });
    And you need to require this in app.js (or the classes that are going to use it) and then you can call this function within the template method in your XTemplate like this:

    Code:
            itemTpl: new Ext.XTemplate(
                '<tpl for=".">',
                    '{[this.returnName(values.name)]}',
                '</tpl>',
             {
                  returnName: function(name) {
                      return MyApp.util.Shared.myFunction(name);
                  }
             })

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,641
    Vote Rating
    898
    Answers
    3573
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      1  

    Default


    What you need to do is really strip out the logic into a sharable class. These classes are called singleton classes. You can create one like:

    Code:
    Ext.define('MyApp.util.Shared', {
        singleton : true,
    
        myFunction : function(name) {
            return 'something ' + name;
        }
    });
    And you need to require this in app.js (or the classes that are going to use it) and then you can call this function within the template method in your XTemplate like this:

    Code:
            itemTpl: new Ext.XTemplate(
                '<tpl for=".">',
                    '{[this.returnName(values.name)]}',
                '</tpl>',
             {
                  returnName: function(name) {
                      return MyApp.util.Shared.myFunction(name);
                  }
             })
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User ksumarine's Avatar
    Join Date
    Feb 2012
    Posts
    16
    Vote Rating
    0
    ksumarine is on a distinguished road

      0  

    Default


    Awesome, I'll try this out and mark it as answered tomorrow when I get to work.

    Thank you!

Thread Participants: 1

Tags for this Thread