Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / app / feed-viewer / app / lib / FeedValidator.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 Ext.define('FV.lib.FeedValidator', {
16     singleton: true,
17     
18     /**
19      * @cfg {String} url The url to validate feeds on
20      */
21     url: 'feed-proxy.php',
22     
23     /**
24      * Validates a given feed's formating by fetching it and ensuring it is well formed
25      * @param {FV.model.Feed} feed The feed to validate
26      */
27     validate: function(feed, options) {
28         options = options || {};
29         
30         Ext.applyIf(options, {
31             scope: this,
32             success: Ext.emptyFn,
33             failure: Ext.emptyFn
34         });
35         
36         Ext.Ajax.request({
37             url: this.url,
38             params: {
39                 feed: feed.get('url')
40             },
41             scope: this,
42             success: function(response) {
43                 if (this.checkResponse(response, feed)) {
44                     options.success.call(options.scope, feed);
45                 }
46             },
47             failure: function() {
48                 options.failure.call(options.scope);
49             }
50         });
51     },
52     
53     /**
54      * @private
55      * Validates that a response contains a well-formed feed
56      * @param {Object} response The response object
57      */
58     checkResponse: function(response, feed) {
59         var dq  = Ext.DomQuery,
60             url = feed.get('url'),
61             xml, channel, title;
62
63         try {
64             xml = response.responseXML;
65             channel = xml.getElementsByTagName('channel')[0];
66             
67             if (channel) {
68                 title = dq.selectValue('title', channel, url);
69                 return true;
70             }
71         } catch(e) {
72         }
73         return false;
74     }
75 });