Skip to content

Instantly share code, notes, and snippets.

@esgy
Created January 28, 2013 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esgy/4653893 to your computer and use it in GitHub Desktop.
Save esgy/4653893 to your computer and use it in GitHub Desktop.
// Utility
if (typeof Object.create !== 'function') {
Object.create = function (obj) {
"use strict";
function F() {}
F.prototype = obj;
return new F();
};
}
(function ($, window, document, undefined) {
var Twitter = {
init: function (options, elem) {
var self = this;
self.elem = elem;
self.$elem = $(elem);
self.url = 'http://search.twitter.com/search.json';
self.search = (typeof options === 'string') ? options : options.search;
self.options = $.extend({}, $.fn.queryTwitter.options, options);
self.refresh(1);
},
refresh: function (length) {
var self = this;
setTimeout(function () {
self.fetch().done(function (results) {
results = self.limit(results.results, self.options.limit);
self.buildFrag(results);
self.display();
if (typeof self.options.onComplete === 'function') {
self.options.onComplete.apply(self.elem, arguments);
}
if (self.options.refresh) {
self.refresh();
}
});
}, length || self.options.refresh);
},
fetch: function () {
return $.ajax({
url: this.url,
data: { q: this.search },
dataType: 'jsonp'
});
},
buildFrag: function (results) {
var self = this;
self.tweets = $.map(results, function (obj, i) {
return $(self.options.wrapEachWith).append(obj.text)[0];
});
},
display: function () {
var self = this;
if (self.options.transition === 'none' || !self.options.transition) {
self.$elem.html(self.tweets); // that's available??
} else {
self.$elem[self.options.transition](500, function () {
$(this).html(self.tweets)[self.options.transition](500);
});
}
},
limit: function (obj, count) {
return obj.slice(0, count);
}
};
$.fn.queryTwitter = function (options) {
return this.each(function () {
var twitter = Object.create(Twitter);
twitter.init(options, this);
$.data(this, 'queryTwitter', twitter);
});
};
$.fn.queryTwitter.options = {
search: '@tutspremium',
wrapEachWith: '<li></li>',
limit: 10,
refresh: null,
onComplete: null,
transition: 'fadeToggle'
};
})(jQuery, window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment