oracle
1 Jul 2009, 9:04 AM
Hi all, i've written a patch to Ext.Component that lets you specify some constructor arguments directly in the placeholder or in the template of the new component.
The code is the following:
Ext.Component.prototype.initComponent = function() {
var parent = Ext.fly(this.renderTo);
if(! parent || ! parent.dom.attributes['ext:config']) {
return;
}
var config = parent.dom.attributes['ext:config'].nodeValue;
var params = Ext.util.JSON.decode(config);
Ext.apply(this, params);
Ext.apply(this.initialConfig, params);
}
You can put this code just at the beginning of your app code.
Here is how it works:
Immagine to have a DOM element in your page that is a place holder for a component, in this element you can add an attribute in some namespaces (eg. ext) and put a json encoded string in.
Example:
<div id="component" ext:config="{width:400,cls:'test'}" ></div>
In this example we'll pass the arguments width: 400 and cls: 'test' to the constructor of the new class.
var txt1 = new Ext.form.TextField({
renderTo: "component"
});
This is equivalent on writing:
var txt1 = new Ext.form.TextField({
renderTo: "component",
width: 400,
cls: 'test'
});
But the advantage is that you don't have to modify your js code to modify the width of your component, everybody will be able to modify that parameter.
Here is a more complex example using a template:
<div id="componentTemplate">
<div style="height:50px;margin-left:20px;width:100%;">
<div id="{subComponntId}" ext:config="{width:400,cls:'toto'}" ></div>
</div>
</div>
and the js:
Ext.app.MyComponent = Ext.extend(Ext.Component, {
onRender: function(ct, position){
Ext.app.MyComponent.superclass.onRender.call(this, ct, position);
var tpl = Ext.Template.from("componentTemplate");
var ids = {
subComponntId: Ext.id()
};
var html = tpl.apply(ids);
this.el.update(html);
var txt1 = new Ext.form.TextField({
text: "test",
renderTo: ids.subComponntId
});
}
});
Please feel free to leave your comments, suggestions and bug reports in this thread.
The code is the following:
Ext.Component.prototype.initComponent = function() {
var parent = Ext.fly(this.renderTo);
if(! parent || ! parent.dom.attributes['ext:config']) {
return;
}
var config = parent.dom.attributes['ext:config'].nodeValue;
var params = Ext.util.JSON.decode(config);
Ext.apply(this, params);
Ext.apply(this.initialConfig, params);
}
You can put this code just at the beginning of your app code.
Here is how it works:
Immagine to have a DOM element in your page that is a place holder for a component, in this element you can add an attribute in some namespaces (eg. ext) and put a json encoded string in.
Example:
<div id="component" ext:config="{width:400,cls:'test'}" ></div>
In this example we'll pass the arguments width: 400 and cls: 'test' to the constructor of the new class.
var txt1 = new Ext.form.TextField({
renderTo: "component"
});
This is equivalent on writing:
var txt1 = new Ext.form.TextField({
renderTo: "component",
width: 400,
cls: 'test'
});
But the advantage is that you don't have to modify your js code to modify the width of your component, everybody will be able to modify that parameter.
Here is a more complex example using a template:
<div id="componentTemplate">
<div style="height:50px;margin-left:20px;width:100%;">
<div id="{subComponntId}" ext:config="{width:400,cls:'toto'}" ></div>
</div>
</div>
and the js:
Ext.app.MyComponent = Ext.extend(Ext.Component, {
onRender: function(ct, position){
Ext.app.MyComponent.superclass.onRender.call(this, ct, position);
var tpl = Ext.Template.from("componentTemplate");
var ids = {
subComponntId: Ext.id()
};
var html = tpl.apply(ids);
this.el.update(html);
var txt1 = new Ext.form.TextField({
text: "test",
renderTo: ids.subComponntId
});
}
});
Please feel free to leave your comments, suggestions and bug reports in this thread.