ComponentLoader.js
6.68 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* This class is used to load content via Ajax into a {@link Ext.Component}. In general
* this class will not be instanced directly, rather a loader configuration will be passed to the
* constructor of the {@link Ext.Component}.
*
* ## HTML Renderer
*
* By default, the content loaded will be processed as raw html. The response text
* from the request is taken and added to the component. This can be used in
* conjunction with the {@link #scripts} option to execute any inline scripts in
* the resulting content. Using this renderer has the same effect as passing the
* {@link Ext.Component#html} configuration option.
*
* ## Data Renderer
*
* This renderer allows content to be added by using JSON data and a {@link Ext.XTemplate}.
* The content received from the response is passed to the {@link Ext.Component#update} method.
* This content is run through the attached {@link Ext.Component#tpl} and the data is added to
* the Component. Using this renderer has the same effect as using the {@link Ext.Component#data}
* configuration in conjunction with a {@link Ext.Component#tpl}.
*
* ## Component Renderer
*
* This renderer can only be used with a {@link Ext.container.Container} and subclasses. It allows for
* Components to be loaded remotely into a Container. The response is expected to be a single/series of
* {@link Ext.Component} configuration objects. When the response is received, the data is decoded
* and then passed to {@link Ext.container.Container#method-add}. Using this renderer has the same effect as specifying
* the {@link Ext.container.Container#cfg-items} configuration on a Container.
*
* ## Custom Renderer
*
* A custom function can be passed to handle any other special case, see the {@link #renderer} option.
*
* ## Example Usage
*
* var cmp = Ext.create('Ext.Component', {
* renderTo: Ext.getBody(),
* tpl: '{firstName} - {lastName}',
* loader: {
* url: 'myPage.php',
* renderer: 'data',
* params: {
* userId: 1
* }
* }
* });
*
* // call the loader manually (or use autoLoad:true instead)
* cmp.getLoader().load();
*/
Ext.define('Ext.ComponentLoader', {
/* Begin Definitions */
extend: 'Ext.ElementLoader',
statics: {
Renderer: {
Data: function(loader, response, active){
var success = true;
try {
loader.getTarget().update(Ext.decode(response.responseText));
} catch (e) {
success = false;
}
return success;
},
Component: function(loader, response, active){
var success = true,
target = loader.getTarget(),
items = [];
//<debug>
if (!target.isContainer) {
Ext.Error.raise({
target: target,
msg: 'Components can only be loaded into a container'
});
}
//</debug>
try {
items = Ext.decode(response.responseText);
} catch (e) {
success = false;
}
if (success) {
target.suspendLayouts();
if (active.removeAll) {
target.removeAll();
}
target.add(items);
target.resumeLayouts(true);
}
return success;
}
}
},
/* End Definitions */
/**
* @cfg {Ext.Component/String} target The target {@link Ext.Component} for the loader.
* If a string is passed it will be looked up via the id.
*/
target: null,
/**
* @cfg {Boolean/Object} loadMask True or a {@link Ext.LoadMask} configuration to enable masking during loading.
*/
loadMask: false,
/**
* @cfg {Boolean} scripts True to parse any inline script tags in the response. This only used when using the html
* {@link #renderer}.
*/
/**
* @cfg {String/Function} renderer
The type of content that is to be loaded into, which can be one of 3 types:
+ **html** : Loads raw html content, see {@link Ext.Component#html}
+ **data** : Loads raw html content, see {@link Ext.Component#data}
+ **component** : Loads child {Ext.Component} instances. This option is only valid when used with a Container.
Alternatively, you can pass a function which is called with the following parameters.
+ loader - Loader instance
+ response - The server response
+ active - The active request
The function must return false is loading is not successful. Below is a sample of using a custom renderer:
new Ext.Component({
loader: {
url: 'myPage.php',
renderer: function(loader, response, active) {
var text = response.responseText;
loader.getTarget().update('The response is ' + text);
return true;
}
}
});
*/
renderer: 'html',
/**
* Set a {Ext.Component} as the target of this loader. Note that if the target is changed,
* any active requests will be aborted.
* @param {String/Ext.Component} target The component to be the target of this loader. If a string is passed
* it will be looked up via its id.
*/
setTarget: function(target){
var me = this;
if (Ext.isString(target)) {
target = Ext.getCmp(target);
}
if (me.target && me.target != target) {
me.abort();
}
me.target = target;
},
// inherit docs
removeMask: function(){
this.target.setLoading(false);
},
/**
* Add the mask on the target
* @private
* @param {Boolean/Object} mask The mask configuration
*/
addMask: function(mask){
this.target.setLoading(mask);
},
setOptions: function(active, options){
active.removeAll = Ext.isDefined(options.removeAll) ? options.removeAll : this.removeAll;
},
/**
* Gets the renderer to use
* @private
* @param {String/Function} renderer The renderer to use
* @return {Function} A rendering function to use.
*/
getRenderer: function(renderer){
if (Ext.isFunction(renderer)) {
return renderer;
}
var renderers = this.statics().Renderer;
switch (renderer) {
case 'component':
return renderers.Component;
case 'data':
return renderers.Data;
default:
return Ext.ElementLoader.Renderer.Html;
}
}
});