ComponentLoader.js
1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Ext JS Library 2.1
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.namespace('Ext.ux');
/**
* @class Ext.ux.ComponentLoader
* Provides an easy way to load components dynamically. If you provide these components
* with an id you can use Ext.ComponentMgr's onAvailable function to manipulate the components
* as they are added.
* @singleton
*/
Ext.ux.ComponentLoader = function() {
var cm = Ext.ComponentMgr;
return {
/*
*
*/
root: 'components',
/*
* 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.createDelegate(this, [config.container], true),
scope: this
});
if (config.container) {
Ext.apply(config.params, {
container: config.container
});
}
Ext.Ajax.request(config);
},
// private
onLoad : function(opts, success, response, ct) {
var config = Ext.decode(response.responseText);
if (config.success) {
var comps = config[this.root];
// loop over each component returned.
for (var i = 0; i < comps.length; i++) {
var c = comps[i];
// special case of viewport, no container to add to
if (c.xtype && c.xtype === 'viewport') {
cm.create(c);
// add to container
} else {
var ct = c.container || ct;
Ext.getCmp(ct).add(c);
Ext.getCmp(ct).doLayout();
}
}
} else {
this.onFailure();
}
},
onFailure: function() {
Ext.Msg.alert('Load failed.');
}
};
}();