Created
March 13, 2011 23:16
-
-
Save walter/868542 to your computer and use it in GitHub Desktop.
Partial refactoring to DRY things up and prepare for upcoming routes.
This file contains 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($) { | |
var app = $.sammy('#main', function() { | |
this.use(Sammy.Handlebars, 'hb'); | |
this.around(function(callback) { | |
var context = this; | |
this.load('data/providers.json') | |
.then(function(providers) { | |
context.providers = providers; | |
}) | |
.then(callback); | |
}); | |
this.get('#/', function(context) { | |
// context.app.swap(''); | |
// get the results from default_source | |
// assumes that first provider's first source isn't a search | |
var defaultSource = this.providers[0].sources[0]; | |
this.trigger('updateProvidersWith', defaultSource); | |
this.trigger('updateResultsFor', defaultSource); | |
}); | |
this.bind('updateProvidersWith', function(e, selectedSource) { | |
context = this; | |
$('#providers-spinner').hide(); | |
$.each(this.providers, function(i, provider) { | |
var providerListId = "provider-list-" + i; | |
$('#providers').append('<h3>' + provider.title + '</h3>'); | |
$('#providers').append("<ul id=\"" + providerListId + "\">"); | |
$.each(provider.sources, function(i, source) { | |
var appropriate_template = 'templates/source'; | |
if (selectedSource && selectedSource == source) { | |
appropriate_template += '_selected'; | |
} | |
if (source.searchable_stub) { | |
appropriate_template += '_search'; | |
} | |
appropriate_template += '.hb'; | |
context.render(appropriate_template, source) | |
.appendTo("#" + providerListId); | |
}); | |
$('#providers').append("</ul>"); | |
}); | |
}); | |
this.bind('updateResultsFor', function(e, source) { | |
context = this; | |
var resultRequest = $.get(source.url) | |
.success(function( response ) { | |
$('#results-spinner').hide(); | |
var itemsLimit = 10; | |
var itemsCount = 0; | |
// items from rss items or atom entries | |
// i want image thumbnail src (enclosure or media:thumbnail) | |
// title | |
// full url (link) | |
var items = $(response).find('item'); | |
// handle Atom | |
if (items.length == 0) { | |
items = $(response).find('entry'); | |
} | |
if (items.length > 0) { | |
$('#results').append("<ul id=\"results-list\">"); | |
items.each(function() { | |
if (itemsCount == itemsLimit) { break; } | |
var resultThumbnail = $(this).find("thumbnail").attr('url'); | |
if (!resultThumbnail || 0 === resultThumbnail.length) { | |
// TODO: currently broken | |
$(this).find('description').each(function() { | |
resultThumbnail = $(this).find('img').attr('src'); | |
}); | |
} | |
var resultTitle = $(this).find('title').text(); | |
var resultLink = $(this).find('link').text(); | |
var result = { | |
title: resultTitle, | |
link: resultLink, | |
thumbnail_url: resultThumbnail | |
} | |
context.render('templates/result_thumbnail.hb', result) | |
.appendTo('#results-list'); | |
itemsCount++; | |
}); | |
$('#results').append("</ul>"); | |
} else { | |
context.render('templates/no_results.hb', source) | |
.appendTo($('#results')); | |
} | |
}) | |
.error(function() { | |
$('#results-spinner').hide(); | |
context.render('templates/results_failed.hb', source) | |
.appendTo($('#results')); | |
}); | |
}); | |
}); | |
$(function() { | |
app.run('#/'); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment