PDA

View Full Version : Sencha example: retrieving data via a local text file rather than a json url proxy.



anthoang
11 Jul 2011, 1:33 PM
Hi,

I am dissecting the Sencha examples to learn how to access data and use it in a Sencha application. Specifically, I am looking at the O'Reilly example http://dev.sencha.com/deploy/touch/examples/oreilly/

My goal is to prevent having to go to a separate url and instead use a text file (or any file) on my local machine.

In the code availabe in the sample, the O'Reilly app


initComponent: function() {
this.list = new Ext.List({
grouped: true,
itemTpl: '<span class="name">{title}</span> <span class="secondary">{room}</span>',
loadingText: false,
store: new Ext.data.Store({
model: 'Proposal',
sorters: 'time',
proxy: {
type: 'scripttag',
url :'https://en.oreilly.com/web2010/public/mobile_app/all',
// url :'mydata.txt', <- this don't work
reader: {
type: 'json',
root: 'proposals'
}
},
listeners: {
load: { fn: this.initializeData, scope: this }
}
});




When I plug the url https://en.oreilly.com/web2010/public/mobile_app/all into my browser, I'm able to extract this information and save it as a text file. But when I try to access text file in my proxy-url with something like this:



proxy: {
type: 'scripttag',
url :'mydata.txt', //<- this don't work
reader: {
type: 'json',
root: 'proposals'
}


The Oreilly list doesn't get the data from the text file, and the Sencha app just hangs with no data to display. I'm wondering with anyone else has tried to access data without having to use an outside url proxy and just a local file. Let me know? thanks.

bharatn
11 Jul 2011, 1:47 PM
For local data use 'ajax' proxy type

anthoang
11 Jul 2011, 2:57 PM
For local data use 'ajax' proxy type

Thank you. I tried what you suggested below. But that wasn't the magic trick. I'm still looking at how to debug data/proxy connections.



proxy: {
type: 'ajax', //<- this didn't work
url :'mydata.json', //<- this don't work
reader: {
type: 'json',
root: 'proposals'
}

dzubi
28 Jul 2011, 1:34 AM
Hi anthoang
I'm stuck with the same problem, have you been able to find a solution?

fx-mike
28 Jul 2011, 1:59 AM
The following works for me


Ext.Ajax.request({
url: 'data.json',
success: function(response, opts) {
console.log(response.responseText);
},
failure: function(response, opts) {
//iOS in phonegap returns response.status=0 on success
if(response.status == 0 && response.responseText != ''){
console.log(response.responseText);
} else {
console.error('failed to load data');
}
}
});


Surely this can be achieved using a proxy

anthoang
28 Jul 2011, 11:06 AM
dzubi,
I've kinda given up.


fx-mike,
Can you provide a bigger snipplet of your code? where/how you are using the Ext.Ajax.request component. Thanks!

fx-mike
29 Jul 2011, 1:20 AM
Well, all I actually do after the call is read the response into a store through a custom reader. But thats a really ugly workaround I need because I have to augment the data with info from a second data source before putting it into the store. Nothing you would normally need to do, thats why I cut down my code ;)

I actually just tried the above code and it works fine for me (tested on safari).
Here is a complete example


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

var s = new Ext.data.Store({
model: 'User',
proxy: {
type: 'ajax',
url :'resources/users.json'
},
listeners: {
load: function(s, r){console.log(r)}
}
});

s.load();




//users.json
[{
"name": "anthoang"
}, {
"name": "mike"
}]

elmogallen
29 Jul 2011, 5:16 AM
Take a look at how I'm doing it over here:
http://www.sencha.com/forum/showthread.php?142054-PhoneGap-Sencha-Touch-loading-local-XML-files

I had to use a combination of jQuery and Sencha Touch. I'm sure that's horrible, but I just could not get it to work any other way.

nakul_sagar
10 Sep 2012, 11:13 PM
Hi everyone..
can we write and read excel file and text file in sencha touch 1 or 2?
If so we can get the data from Excel file and stores into database pragmatically, which makes easy to update database through excel file instead of modifying in DB and again installing application.

Note: I am talking about mobile application which is not web app

Thanks in advance..