-
28 Sep 2007 11:50 PM #1
[Ext 2.0] Ext.ux.ComponentLoader
[Ext 2.0] Ext.ux.ComponentLoader
I am experimenting with loading my components via server-side json which non-developers can easily manipulate. My first pass at a ComponentLoader is here:
An Example of how you would use it:Code:Ext.namespace('Ext.ux'); /** * @class Ext.ux.ComponentLoader * @author Aaron Conran * Provides an easy way to load components dynamically. If you name these components * you can use Ext.ComponentMgr's onAvailable function to manipulate the components * as they are added. * @singleton */ Ext.ux.ComponentLoader = function() { var defaultXType = 'panel'; var cm = Ext.ComponentMgr; return { /** * Load components from a server resource, config options include anything available in @link Ext.data.Connect#request * Note: Always uses the connection of Ext.Ajax */ load : function(config) { Ext.apply(config, { callback: this.onLoad, scope: this }); Ext.Ajax.request(config); }, // private onLoad : function(opts, success, response) { var config = Ext.decode(response.responseText); for (var i = 0; i < config.components.length; i++) { var comp = cm.create(config.components[i], config.defaultXType[i] || defaultXType) } } }; }();
And finally the JSON packet that the load received via the coldfusion request.Code:Ext.ux.ComponentLoader.load({url: 'loadComponents.cfm'}); Ext.ComponentMgr.onAvailable('bookTree', function(bookTree) { bookTree.root.appendChild( new Ext.tree.TreeNode({text: 'JavaScript: The Definitive Guide', allowDrag: false}), new Ext.tree.TreeNode({text: 'Pro JavaScript Techniques', allowDrag: false}), new Ext.tree.TreeNode({text: 'Visual C# 2005 Recipes', allowDrag: false}) ); bookTree.on('click', function(node, e) { var inner = String.format('Please place a nice review for {0} here.', node.attributes.text); Ext.getCmp('content').body.update(inner); }); });
I will be putting together an article on how to use the extension and some of the benefits/disadvantages of loading your components this way.Code:{ defaultXType: 'panel', components: [ { xtype: 'viewport', layout: 'border', items: [{ region: 'north', border: false, height: 100, split: false, html: 'Header' },{ layout: 'border', region: 'center', border: false, items: [{ xtype: 'treepanel', region: 'west', width: 320, id: 'bookTree', rootVisible:false, root: new Ext.tree.TreeNode(), title: 'Books' },{ id: 'content', region: 'center', html: 'sample' }] },{ region: 'south', height: 24, title: 'Dynamic Loader Demo' }] } ] }
Aaron
-
29 Sep 2007 10:00 AM #2
Cool idea Aaron. Looking forward to the accompanying article.
-
30 Sep 2007 11:35 AM #3
Thanks for ux!
But how I can execute handlers?
Thanks.PHP Code:'handler' => 'function() {alert("dsf")}'
// -> this.handler.call is not a function
-
30 Sep 2007 1:50 PM #4
is i t possible to define components to use inside other components?
something like
Code:{ defaultXType: 'panel', east:{ title:'east', region:'east' }, components: [{ xtype: 'viewport', layout: 'border', items: [ { layout: 'border', region: 'center', border: false, items: [{ xtype: 'treepanel', region: 'west', width: 320, id: 'bookTree', rootVisible:false, root: new Ext.tree.TreeNode(), title: 'Books' },this.east,{ id: 'content', region: 'center', html: 'sample' }] },{ region: 'south', height: 24, title: 'Dynamic Loader Demo' }] } ] }
-
30 Sep 2007 5:23 PM #5
mlarese -
Why would you want to do that as opposed to placing the east config inside the components array? I suppose if you were going to use a similar configuration it may be useful. However, being as this JSON is expected to be generated via a server-side component I would expect that to be implemented on the server-side.
Aaron
-
30 Sep 2007 5:25 PM #6
6epcepk -
What do you mean by execute handlers? This extension is primarily to load components via server-side JSON. You can manipulate/use the components after they are loaded. To determine when a specific component is loaded use onAvailable of the Ext.ComponentMgr singleton as shown in the above sample.
Aaron
-
30 Sep 2007 9:44 PM #7
@aconran
it was just an example.
the problem is that a json object if is to big is difficult to write and debug.
It is mor easy to write pieces of it and compose the big one.
-
1 Oct 2007 12:42 AM #8
-
1 Oct 2007 4:00 AM #9
IMHO you could use something like this
Ext.get('yourComponentId').on('click', doSomething);
or maybe with Ext.getCmp instead of Ext.get...
-
25 Apr 2008 9:04 AM #10
Further example
Further example
Hi,
I note in all the examples it tends to revolve around a viewport....
However, I have something else I would like to do with it - is this possible?
I have a viewport defined statically in a js (which I want), with a toolbar as one of my regions - can I use component loader to load toolbar items into it?
Thanks
James


Reply With Quote
