]>
git.proxmox.com Git - sencha-touch.git/blob - src/examples/ajax/app.js
8 * This application demonstrates the simple AJAX abilities of Sencha Touch.
10 * We setup a simple container which has a 2 buttons which will trigger a function to either make
11 * a AJAX request using Ext.Ajax.request or a JSONP request using Ext.data.JsonP.
14 //the first thing we do is define out application
17 '320x460': 'resources/startup/Default.jpg', // Non-retina iPhone, iPod touch, and all Android devices
18 '640x920': 'resources/startup/640x920.png', // Retina iPhone and iPod touch
19 '640x1096': 'resources/startup/640x1096.png', // iPhone 5 and iPod touch (fifth generation)
20 '768x1004': 'resources/startup/768x1004.png', // Non-retina iPad (first and second generation) in portrait orientation
21 '748x1024': 'resources/startup/748x1024.png', // Non-retina iPad (first and second generation) in landscape orientation
22 '1536x2008': 'resources/startup/1536x2008.png', // : Retina iPad (third generation) in portrait orientation
23 '1496x2048': 'resources/startup/1496x2048.png' // : Retina iPad (third generation) in landscape orientation
26 isIconPrecomposed
: false,
28 57: 'resources/icons/icon.png',
29 72: 'resources/icons/icon@72.png',
30 114: 'resources/icons/icon@2x.png',
31 144: 'resources/icons/icon@144.png'
34 //requires defines the Components/Classes that our application requires.
47 * The launch method is called when the browser is ready, and the application can launch.
49 * Within this function we create two a new container which will show the content which
50 * is returned when we make an AJAX request. We also have a toolbar docked to the top which
51 * has buttons to trigger the AJAX requests. And finally we have a toolbar docked to the bottom
52 * which shows the status of the current request.
56 //define each of the items inside this container
60 //we give it an id so we can refrence it below
63 //the content can be scrolled, so set the scrollable config to true
66 //we give it a cls so we can custom style it using CSS
69 //we set the default html to something we the user knows what is happening
70 html
: 'This example can use either JSONP or AJAX to retrieve data.'
73 //this is the top toolbar which will show the two buttons to make a request
75 //we give it an xtype of titlebar. titlebar allows you to use 'align' in
76 //it's children so we can align them to the left/right. see directly below
79 //we want the bar to be docked at the top
82 //and now we define the items we want inside the toolbar
85 //the text displayed on the button
88 //next we give this button it's handler, which is a link to the function
89 //we want to call when this button is tapped. we also give it a scope so
90 //it knows to call this function within the scope of our application
91 handler
: this.makeJSONPRequest
,
94 //align the button to the left
98 //the text of the button
101 //once again we give it a link to the function we want to call when we tap
102 //the button. this time it calls the Ajax method
103 handler
: this.makeAjaxRequest
,
106 //and then align to the right
112 //now we define the bottom toolbar.
114 //this time we just use toolbar, as we dont need to align buttons within it
117 //give it an id so we can reference it later
120 //do it to the bottom
123 //give it it's default title
124 title
: 'Tap a button above.',
126 //and we give it a ui of 'light' (the default is dark) so it looks a little difference
132 //we set application variables so we don't have to keep finding a reference to both the contentView
133 //and statusView each time we need to update them (below).
134 //we use Ext.getCmp which allows us to find a component from an id (which we specified above)
135 this.contentView
= Ext
.getCmp('contentView');
136 this.statusView
= Ext
.getCmp('statusView');
140 * This makes an AJAX request using Ext.Ajax.request. When it completes, it updates the contentView's
141 * html configuration with the responseText (data from the loaded file).
143 makeAjaxRequest: function() {
144 //firstly we set local variables of the contentView and statusView. we generallt do this
145 //only if we are going to use it more than once in our function
146 var contentView
= this.contentView
,
147 statusView
= this.statusView
;
149 //next we call the setter of the masked configuration in our contentview. we give it an option
150 //which tells it to use a Ext.LoadMask which display a loading indicator, and will also display
151 //the provided message
152 contentView
.setMasked({
154 message
: 'Loading...'
157 //now we create the Ajax request
159 //first we give it the URL of the request. take not that this can only be local to the web server
163 //then we define a success method, which is called once the ajax request is successful
164 success: function(response
) {
165 //the first argument returned is the reponse object, and that object has a property called
166 //responseText which is the text of the file we just loaded. so we call setHtml which
167 //will update the contentView with the text of the response
168 contentView
.setHtml(response
.responseText
);
170 //now we set the title of the statusView component to say we loaded the json file
171 statusView
.setTitle('Static test.json file loaded');
173 //and finally we unmask the contentView so the content is viewable
174 contentView
.unmask();
176 failure: function() {
177 contentView
.unmask();
183 * This method makes a JSONP request using Ext.data.JsonP. JSONP is used when you need to make cross-domain
184 * requests. otherwise you can use AJAX (above).
186 * When the request completes, it will display weather information for the specified location in the
189 makeJSONPRequest: function() {
190 //once again we set local variables of the contentView and statusView because we need to reference them
191 //more than once. we also get the XTemplate instance which we will used to display weather information.
192 var tpl
= this.getWeatherTemplate(),
193 contentView
= this.contentView
,
194 statusView
= this.statusView
;
196 //first we set the mask of the contentView to show a loading message
197 contentView
.setMasked({
199 message
: 'Loading...'
202 //next we use Ext.data.JsonP to make a request
203 Ext
.data
.JsonP
.request({
204 //we give it the url to the free worldweatheronline.com api
205 url
: 'http://api.worldweatheronline.com/free/v1/weather.ashx',
207 //the callbackKey is used for JSONP requests
208 callbackKey
: 'callback',
210 //now we define the params to be sent to the server
212 //first it is the API key so we can use the site
213 key
: 'qfj4gk3t4u5u3bqc8atf69fn',
215 //nexgt is the `q` param which is a valid US zipcode (palo alto in this case)
218 //next we define the format, json
221 //and finally the number of days we want
225 //now we define a callback method which is called when the JSONP response is successful.
226 success: function(result
) {
227 //the result is a json object which is returned by the API we just requested.
228 //in this case all we want is the data.weather property, which is an array
229 var weather
= result
.data
.weather
;
231 //now we check if the weather is actually defined
233 //if it is defined, we use the setHtml method on contentView to update the html
234 //using the tpl.apply method.
235 //Ext.XTemplate.apply will bind the data you pass into the xtemplate provided
236 //and return a string
237 contentView
.setHtml(tpl
.apply(weather
));
239 //now we set the title of the status bar
240 statusView
.setTitle('Weather: Palo Alto, CA');
242 //if it wasn't defined, we throw an error using Ext.Msg.alert()
243 Ext
.Msg
.alert('Error', 'There was an error retrieving the weather.');
246 //and finally unmask the content view
247 contentView
.unmask();
249 failure: function() {
250 Ext
.Msg
.alert('Error', 'There was an error retrieving the weather.');
251 contentView
.unmask();
257 * Returns a Ext.XTemplate instance which will be used to display each weather result when makeJSONPRequest
259 * @return {Ext.XTemplate} The returned template
261 getWeatherTemplate: function() {
262 return new Ext
.XTemplate([
265 '<div class="date">{date:date("M d, Y")}</div>',
266 '<div class="icon">',
267 '<tpl for="weatherIconUrl">',
268 '<img src="{value}" />',
271 '<div class="temp">{tempMaxF}°<span class="temp_low">{tempMinF}°</span></div>',