-
21 Dec 2010 12:15 PM #11
Oops, you're right, I made a mistake. Replace this line:
with this line.Code:wrapperId += initialConfig.id;
It should work then. Or, you could use the new version of the override. It's posted below.Code:wrapperId += initConfig.id;
You see, once I was able to add components in a template, I wanted to add a whole form and all its child fields. It was doable with the previous override but the instruction would have been incredibly long since it's limited to be one-line. Now, I can also do the following:
Basically, I added a new method addCmpTo(parentComponentId, initConfigObject) which makes it easier to append children to a previously added component. Here's the new override.Code:{[ this.addCmp({id:"myform", xtype:"formpanel" }) ]} {[ this.addCmpTo("myform", {xtype:"textfield", label:"First Name"}) ]} {[ this.addCmpTo("myform", {xtype:"textfield", label:"Last Name"}) ]}
Code:(function () { var originalOverwrite = Ext.XTemplate.prototype.overwrite; Ext.override(Ext.XTemplate, { overwrite: function () { //execute the base overwrite method originalOverwrite.apply(this, arguments); //then, render any components that addCmp might have added while (this.templateComponents.length > 0) { var el = this.templateComponents.shift(); if (Ext.get(el.renderTo) != null) { var newel = new Ext.create(el); } } }, //an array to store the initialconfigs of the components-to-be templateComponents:[], templateIdCount:0, //Pushes an initialConfig to the array so that //a component can be later created and rendered. //It also generates a div container for the component //with a random id if no id was provided addCmp: function (initConfig) { var wrapperId = "ext-comp-wrapper-"; if (initConfig.id != void (0)) { wrapperId += initConfig.id; } else { //wrapperId += Math.random().toString().replace(".", ""); wrapperId += (this.templateIdCount++).toString() + Math.random().toString().replace(".", ""); } initConfig.renderTo = wrapperId; this.templateComponents.push(initConfig); //return '<div id="' + wrapperId + '"' + (initConfig.width == void(0) ? '' : ' style="width:'+ initConfig.width +'px"') + '></div>'; return '<div id="' + wrapperId + '" class="y-wrapper"></div>'; }, addCmpTo: function (container, initConfig, collection) { if (collection == void(0)){ this.addCmpTo(container, initConfig, this.templateComponents); } else { if (collection.length > 0){ for (var i=0; i<collection.length; i++){ if (collection[i].id == container){ if (collection[i].items == void(0)){ collection[i].items = []; } collection[i].items.push(initConfig); break; } if (collection[i].items != void(0)){ this.addCmpTo(container, initConfig, collection[i].items); } } } } return; } }); })();
-
21 Dec 2010 12:18 PM #12
-
1 Jan 2011 12:20 AM #13
is something similar going to be included in future sencha touch releases?
seems like a much needed capability...
can someone from the sencha team comment?
thanks
-
8 May 2011 5:35 PM #14
This is a bit dirty (at least in terms of abusing Templates), but you can use a closure for this:
BrightSoul, as others have said, this extension is awesome.Code:(function() { // capture scope var me = this; var container = new Ext.Container({ tpl: new Ext.XTemplate( '<div>', '{[ this.addCmp({xtype:"button", text: "Fire", handler: this.fire }) ]}', '</div>', { fireEvent: function() { // use scope me.fireEvent('eventName'); } } ) }); })();Last edited by AlexKorn; 8 May 2011 at 5:37 PM. Reason: Hadn't finished post. "Preview" will actually do the post if you need to log in again.
-
29 Aug 2011 4:45 PM #15
I'd like to use tpl variables inside my added component. Has anyone done this?
Code:'{[ this.addCmp({xtype:"button", text: "{text}", handler: this.fire }) ]}',
The above code renders this HTML:
HTML Code:<span class="x-button-label" id="ext-gen1136">',(values['text'] === undefined ? '' : values['text']),'</span>
-
30 Aug 2011 4:07 PM #16
-
15 Nov 2011 12:51 AM #17
The code works, except.. it keeps adding the button to the first line, and not the line that I want it added to :S
Here is my template:
Any help with that? Also when I click the button it fires the event 7! times. Is that supposed to happen? ^^Code:new Ext.XTemplate( '<div class="memo">', '<div class="memo-type">{[this.GetWarehouseName(values.WarehouseID)]}</div>', '<div class="memo-title">{ArticleName} ({Amount})</div>', '<tpl if="Selected">{ArticleDescription}</tpl>', '</div>', '{[ this.addCmp({id:"removebutton", xtype:"button", cls: "remove_title", width: "30px", height: "30px", border: 0, style: { border: "0px" }, handler: this.fireEvent}) ]}', { GetWarehouseName: function (id) { var whrstore = Ext.StoreMgr.get('Warehouses'); var warehouse = whrstore.findRecord('ID', id); return warehouse.get('WarehouseName'); }, fireEvent: function () { console.log("FEUERRR!"); } }
Edit: One other thing I noticed is that when I change the Selected property of the record, the added button dissapears..
-
31 Jan 2012 4:56 PM #18
This is a convenient override, but I think one needs to watch out for timing issues with it. I'm displaying my button in a dataview that is redrawn whenever data in the store changes. I've found that the button doesn't always display because the wrapper hasn't been inserted in the DOC before overwrite() executes.
I put the Ext.create call inside a defer() and it worked.
Also, I had to add the xtype to the create() arguments as the first parameter. (I don't know why others haven't run into errors with the create() call. Maybe you all are using a different version of Ext than I am? I'm using 4.0 and writing a desktop app, not using Sencha Touch). Also, the 'new' is not necessary in front of the create() call.
Here's what my create call looks like now:where xtype is of the form "Ext.Button"Code:var cfg = this.templateComponents.shift(); if (Ext.get(cfg.renderTo) != null) { Ext.defer(Ext.create, 1, this, [cfg.xtype, cfg]); }
-
22 Feb 2012 2:36 PM #19
This is a great plugin, I use it all the time. However, I constantly have an issue with itemselector since every time I put the button into my list or dataview, the button tap also triggers itemselector. Is there any way to have an exception to itemselector?
-
18 Apr 2012 1:21 AM #20
Thanks a lot Vital Aaron, I save a lot of time tkanks to the bit of code you provided.
I was struggling to implement those features in Touch 2Last edited by pepperseb; 18 Apr 2012 at 1:22 AM. Reason: typo
Similar Threads
-
DataView Template with button
By tzander in forum Ext 2.x: Help & DiscussionReplies: 4Last Post: 10 Apr 2013, 11:23 AM -
button rendering problem with own template
By bernd01 in forum Ext 3.x: Help & DiscussionReplies: 0Last Post: 3 Jun 2010, 1:37 PM -
Button sprite template
By deanna in forum Ext 3.x: Help & DiscussionReplies: 4Last Post: 3 Mar 2010, 8:07 AM -
Is it possible to insert button into template?
By atchijov in forum Ext 3.x: Help & DiscussionReplies: 4Last Post: 19 Jul 2009, 1:27 AM -
[FIXED][1.1] Little bug in Button Template ?
By lioledingue in forum Ext GWT: Bugs (1.x)Replies: 1Last Post: 6 Oct 2008, 2:20 PM



Reply With Quote