-
1 Feb 2013 12:58 PM #1
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.
Sorry if this has been posted elsewhere, I tried searching and didn't see anything.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; } }) } ]
Thanks!
-
Best Answer Posted by mitchellsimoens
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:
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:Ext.define('MyApp.util.Shared', { singleton : true, myFunction : function(name) { return 'something ' + name; } });
Code:itemTpl: new Ext.XTemplate( '<tpl for=".">', '{[this.returnName(values.name)]}', '</tpl>', { returnName: function(name) { return MyApp.util.Shared.myFunction(name); } })
-
3 Feb 2013 1:51 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
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:
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:Ext.define('MyApp.util.Shared', { singleton : true, myFunction : function(name) { return 'something ' + name; } });
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
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
3 Feb 2013 1:54 PM #3
Awesome, I'll try this out and mark it as answered tomorrow when I get to work.
Thank you!


Reply With Quote