Skip to content

Instantly share code, notes, and snippets.

@walter
Created March 1, 2011 01:43
Show Gist options
  • Save walter/848451 to your computer and use it in GitHub Desktop.
Save walter/848451 to your computer and use it in GitHub Desktop.
broken refactoring for DRYness of updateResultsFor to be bound
(function($) {
var app = $.sammy('#main', function() {
this.use(Sammy.Handlebars, 'hb');
// this.use('Template');
// get providers
// append provider title
// create ul
// render each of the sources of each provider as li
// use the correct template for the source type
// depending on whether the url is a searchable_stub
this.around(function(callback) {
var context = this;
this.load('data/providers.json')
.then(function(providers) {
context.providers = providers;
})
.then(callback);
});
// for sources that are search inputs
// take submited search terms
// and request results from source url + search terms
this.get('#/', function(context) {
// context.app.swap('');
$('#providers-spinner').hide();
// different template for source_selected.hb
// TODO: add logic that looks up current source
// TODO: add logic updates resultRequest with selected source
// route = #/id (id of source)
// route = #/id (id of source) + params[search_terms] for searchable source
// route for results = #/results/id where id is url
// add to results:
// or upload image -> requests url for upload and returns to #/results/id
// or add image URL
// add to result detail page a "back" button
$.each(this.providers, function(i, provider) {
$('#providers').append('<h3>' + provider.title + '</h3>');
$('#providers').append("<ul>");
$.each(provider.sources, function(i, source) {
var appropriate_template = 'templates/source';
if (source.searchable_stub) {
appropriate_template += '_search';
}
appropriate_template += '.hb';
context.render(appropriate_template, source)
.appendTo($('#providers'));
});
$('#providers').append("</ul>");
});
// get the results from default_source
// assumes that first provider's first source isn't a search
var defaultSource = this.providers[0].sources[0];
context.trigger('updateResultsFor', [defaultSource]);
});
// get each entries up to limit
// render in template for result
// append to #results
// animate the results each being added
this.bind('updateResultsFor', function(context, source) {
var resultRequest = $.get(source.url)
.success(function( response ) {
$('#results-spinner').hide();
// TODO: replace hardcoded limit with configuration lookup
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');
}
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.partial('templates/result_thumbnail.hb', result)
.appendTo($('#results'));
itemsCount++;
});
})
.error(function() {
$('#results-spinner').hide();
context.render('templates/results_failed.hb', defaultSource)
.appendTo($('#results'));
});
});
});
$(function() {
app.run('#/');
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment