PDA

View Full Version : switched on XmlWriter now getting "documentRoot is not defined"



Steve Murphy
22 Nov 2009, 5:08 PM
Working from the EditableGrid example, I had everything working just fine - even reading data using an XmlReader from my Java Spring-based controller. I took the logical next step, which was to turn on data writes for updates to the server (ultimately to the DB, of course).

So I included the line:


var writer = new Ext.data.XmlWriter();and included the writer as follows:



var store = new Ext.data.Store({
storeId: 'plants',
autoDestroy: true,
proxy: proxy,
reader: reader,
writer: writer,
sortInfo: {field:'description', direction:'ASC'}
});
The proxy is as follows:



var proxy = new Ext.data.HttpProxy({
api: {
create : 'plants/create',
read : 'plants/read',
update : 'plants/update',
destroy : 'plants/destroy'
}
});
I can edit a field, but when I tab or click out of the field, I get the following error in firebug:

documentRoot is not defined (line ext-all-debug.js (line 12575))

It doesn't get to the update on the server (verified by logging), so I'm not sure what the problem is. I looked for an example or some code to follow, but I'm having problems finding anything for writing back to the server using XmlStore.

I'm not above a quick answer, but if you have a good example to point me too that would be okay too. I'm really looking to understand the root cause of the problem and how to resolve it, learning something more about XmlWriter along the way.

Digitalman
23 Nov 2009, 12:09 PM
After searching code it seems to have something to do with tpl. The only place in Ext debug code that found references to documentRoot was in tpl code. Don't know anything about the tpl stuff though.

Steve Murphy
23 Nov 2009, 8:45 PM
Here's an odd thing, I switch the writer from an XmlWriter to a JsonWriter and it works:



writer = new Ext.data.JsonWriter({
encode: true,
writeAllFields: false
});


The line in ext-debug that seems to habor the problem is:


tpl: '<tpl for="."><' + '?xml version="{version}" encoding="{encoding}"?' + '><tpl if="documentRoot"><{documentRoot}><tpl for="baseParams"><tpl for="."><{name}>{value}</{name}</tpl></tpl></tpl><tpl if="records.length&gt;1"><{root}></tpl><tpl for="records"><{parent.record}><tpl for="."><{name}>{value}</{name}></tpl></{parent.record}></tpl><tpl if="records.length&gt;1"></{root}></tpl><tpl if="documentRoot"></{documentRoot}></tpl></tpl>',


It's the only documentRoot I can find anywhere. Am I supposed to do something else in order to use XmlWriter?

Another thing I thought odd was that even though I was updating a record, the JsonWriter called the create API from the HttpProxy instead of the update. Not sure why this is. Something seems very off but I can't put my finger on it. I think I'm following the correct procedure here but I still feel I'm missing something.

jlopez
27 Nov 2009, 4:29 PM
Here the problem is that the default tpl includes some variables that don't really exist (at least on the code I've been able to access and understand), so you should use a different one with your Writer passing it as you instantiate it. So, for avoiding these problems with documentRoot and baseParams, you could do something simple like this:


new Ext.data.XmlWriter({root: 'records', tpl:'<tpl for="."><' + '?xml version="{version}" encoding="{encoding}"?' + '><tpl if="records.length&gt;0"><tpl if="root"><{root}><tpl for="records"><tpl if="fields.length&gt;0"><{parent.record}></tpl><tpl for="fields"><{name}>{value}</{name}></tpl></{parent.record}></tpl></tpl><tpl if="root"></{root}></tpl></tpl></tpl>', ... } ...)With this you would obtain an XML with the following structure:


<?xml version="{version}" encoding="{encoding}"?><records><{parent.record}><{firstFieldName}>{firstFieldValue}</{firstFildName}><{secondFieldName}>{secondFieldValue}</{secondFieldName}>...</{parent.record}><{parent.record}>...</records>I hope this helps, and consider making your own tpl if this doesn't cover all your needs. ;)

Steve Murphy
27 Nov 2009, 5:29 PM
It helped as I will tuck this nugget of info away in a good place. I did, however, need to make forward progress so I switch over to JSON, which after reading in the Grid FAQ (I think it was) that there is a performance penalty when dealing with XML vs. JSON (makes sense I suppose).

Your example was easy enough to understand, in general, but I didn't understand the need for tpl in the XmlWriter object or why I needed to provide my own. Also didn't find it documented in XmlWriter which makes it particularly difficult to get my hands around.

But, I am happy to say that JsonWriter works nicely.

jlopez
28 Nov 2009, 1:23 AM
Json is created in a more direct way in Json Writer, and as it's lighter it makes sense you prefer it.

About the tpl, I know it's not easy at first, and some documentation of the XmlWriter is not updated or complete yet, so it's even more difficult to deal with it. Anyway, it makes sense that people can pass their own tpl as you may need an specific xml structure (for example, I needed to pass a common field in all records that wasn't stored in them), but I don't know why a tpl that is supposed to work by default but it doesn't was included in it.

I hope next revision will have a complete documentation of this class.