Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / app / feed-viewer / app / lib / FeedValidator.js
1 Ext.define('FV.lib.FeedValidator', {
2     singleton: true,
3     
4     /**
5      * @cfg {String} url The url to validate feeds on
6      */
7     url: 'feed-proxy.php',
8     
9     /**
10      * Validates a given feed's formating by fetching it and ensuring it is well formed
11      * @param {FV.model.Feed} feed The feed to validate
12      */
13     validate: function(feed, options) {
14         options = options || {};
15         
16         Ext.applyIf(options, {
17             scope: this,
18             success: Ext.emptyFn,
19             failure: Ext.emptyFn
20         });
21         
22         Ext.Ajax.request({
23             url: this.url,
24             params: {
25                 feed: feed.get('url')
26             },
27             scope: this,
28             success: function(response) {
29                 if (this.checkResponse(response, feed)) {
30                     options.success.call(options.scope, feed);
31                 }
32             },
33             failure: function() {
34                 options.failure.call(options.scope);
35             }
36         });
37     },
38     
39     /**
40      * @private
41      * Validates that a response contains a well-formed feed
42      * @param {Object} response The response object
43      */
44     checkResponse: function(response, feed) {
45         var dq  = Ext.DomQuery,
46             url = feed.get('url'),
47             xml, channel, title;
48
49         try {
50             xml = response.responseXML;
51             channel = xml.getElementsByTagName('channel')[0];
52             
53             if (channel) {
54                 title = dq.selectValue('title', channel, url);
55                 return true;
56             }
57         } catch(e) {
58         }
59         return false;
60     }
61 });