Last active
August 29, 2015 14:13
-
-
Save kunukn/fc8e7dee29c0a94ce152 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !function(d, $) { | |
| var stub = { | |
| entries: [{ | |
| title: 'title 1', | |
| link: 'link 1', | |
| author: 'author 1', | |
| publishedDate: 'publishedDate 1', | |
| categories: ['category 11', 'category 12'] | |
| }, { | |
| title: 'title 2', | |
| link: 'link 2', | |
| author: 'author 2', | |
| publishedDate: 'publishedDate 2', | |
| categories: ['category 21', 'category 22'] | |
| }] | |
| }; | |
| // Aggregate string array to string | |
| Handlebars.registerHelper('categoriesFormat', function(categories) { | |
| if (!categories || categories.length === 0) return ''; | |
| if (categories.length === 1) return categories[0]; | |
| return categories.reduce(function(a, b) { | |
| return a + b + ', '; | |
| }, ''); | |
| }); | |
| // Format author data | |
| Handlebars.registerHelper('authorFormat', function(author) { | |
| return author ? '- ' + author : '- Unknown author'; | |
| }); | |
| var | |
| templateSource = $('#template').html(), | |
| template = Handlebars.compile(templateSource), | |
| html = '', | |
| // Json url | |
| url = '//ajax.googleapis.com/ajax/services/feed/load?v=1.0'; | |
| url += '&num=10&q=http://news.google.com/news?output=rss'; | |
| // Get json data from Google | |
| $.ajax({ | |
| url: url, | |
| type: 'GET', | |
| // Avoid having no CORS policy issues by using jsonp | |
| // I trust Google will do no evil | |
| dataType: 'jsonp', | |
| success: function(result) { | |
| // If download was success, then apply rendering | |
| if (result.responseStatus === 200) { | |
| html = template(result.responseData.feed); | |
| } else { | |
| html = template(stub); | |
| } | |
| }, | |
| error: function(result) { | |
| html = template(stub); | |
| console.log(result); | |
| $('body').append('ERROR - ' + JSON.stringify(result)); | |
| }, | |
| complete: function() { | |
| $('#rendered').append(html); | |
| } | |
| }); | |
| }(document, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment