PDA

View Full Version : Shall i use Json or AJax here ?



nileshbhd5
1 Jan 2011, 5:25 AM
Hi,

I am a newbie to Sencha Touch and Json, hope someone helps me out here.

I want to read a URL, which returns the following XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:categories xmlns:ns2="http://www.abc.com/tar">
<category catId="96" title="News"/>
<category catId="97" title="Articles">
<category catId="101" title="Destinations"/>
<category catId="213" title="New Destinations"/>
</ns2:categories>

So my questions are

1) How do i send the External url request ?
2) How to read the Data and Display the above categories in List View

Bucs
1 Jan 2011, 9:53 AM
You can send the request like so:



Ext.Ajax.request({
url: 'someURL',
params: {
param1: 'test'
},
success: function (response, opts) {
// Decode the response into a JSON object
jsonResponse = Ext.decode(response.responseText);

}
});


I personally always return JSON from my apps and haven't had the need to deal with consuming XML responses yet, but you would basically need to return your XML document from the server call and the Content-Type header would have to be set to "text/xml", then you would need to configure an XML Reader to read the response into the XML Reader object. You can read the details here (http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.XmlReader).

nileshbhd5
3 Jan 2011, 9:20 PM
Here are more details

1) I have a data.xml file which looks like below ::


<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>1</id>
<name>Ed Spencer</name>
<email>ed@sencha.com</email>
</user>
<user>
<id>2</id>
<name>Abe Elias</name>
<email>abe@sencha.com</email>
</user>

2) What i want to do here is read this XML data

3 ) I used the following code For reading the Data, but its not displaying any result, can anyone help and let me know what is wrong here ?




Ext.regModel('User', {
fields: ['name', 'email']
});

topicsStore = new Ext.data.Store({
model: 'User',
proxy: {
type: 'ajax',
url : 'data.xml',
reader: {
type: 'xml',
model: 'User',
record: 'user'

}
}

});


topics = new Ext.Panel({
iconCls: 'favorites',
title:'topics',
cls: 'demo-list',
items: [{

xtype: 'list',
disclosure: {
scope: topicsStore,
handler: function(record, btn, index) {
alert('Disclose more info for ' + record.get('newsTitle'));
}
},
store: topicsStore,
tpl: '<tpl for="."><div class="class="contact""><table><tr><td width="70" style="font-size:20px; text-align:center; font-weight:bold">{name}</td><td><strong>{name}</strong><br>{email}</td></tr></table></div></tpl>',
itemSelector: 'div.contact'
//singleSelect: true
}]
})

cybermerlin
4 Jan 2011, 5:32 AM
xml should have one root


<? xml .. ?>
<users>
<user>..</user>
<user>..</user>
</users>

n mb this code for you're store:

topicsStore = new Ext.data.Store({
url : 'data.xml',
model: 'User',
reader: new Ext.data.XmlReader({
record: 'user',
id: 'id',
model: 'User'
})
});

jay@moduscreate.com
4 Jan 2011, 12:45 PM
you should not compare JSON with Ajax. two totally different things.

nileshbhd5
5 Jan 2011, 3:47 AM
IT still displays Blank, doesnt read the XML file, how do i debug it ?

Here is the XML code


<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>1</id>
<name>Ed Spencer</name>
<email>ed@sencha.com</email>
</user>
<user>
<id>2</id>
<name>Abe Elias</name>
<email>abe@sencha.com</email>
</user>

Here is the topics.js File




Ext.regModel('User', {
fields: ['name', 'email']
});


topicsStore = new Ext.data.Store({
url : 'data.xml',
model: 'User',
reader: new Ext.data.XmlReader({
record: 'user',
id: 'id',
model: 'User'
})
});

topics = new Ext.Panel({
iconCls: 'favorites',
title:'topics',

layout: Ext.is.Phone ? 'fit' : {
type: 'vbox',
align: 'center',
pack: 'center'
},
cls: 'demo-list',
items: [{

width: Ext.is.Phone ? undefined : 300,
height: 500,
xtype: 'list',
disclosure: {
scope: topicsStore,
handler: function(record, btn, index) {

}
},
store: topicsStore,
tpl: '<tpl for="."><div class="class="contact""><table><tr><td width="70" style="font-size:20px; text-align:center; font-weight:bold">{name}</td><td><strong>{name}</strong><br>{email}</td></tr></table></div></tpl>',
itemSelector: 'div.contact'

//singleSelect: true
}]
})

cybermerlin
5 Jan 2011, 10:28 AM
IT still displays Blank, doesnt read the XML file, how do i debug it ?

Here is the XML code

Code:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>1</id>
<name>Ed Spencer</name>
<email>ed@sencha.com</email>
</user>
<user>
<id>2</id>
<name>Abe Elias</name>
<email>abe@sencha.com</email>
</user>
i wrote you - use one root node into xml !!!! example look above

or look example sencha sencah_example (http://dev.sencha.com/deploy/dev/examples/grid/sheldon.xml)

BurhanB
7 Dec 2012, 10:58 PM
Hi , i am new to sencha touch and want to know how to use JSON parsing in sencha touch.. A simple working example will be admired ..
Thanx in advance