Skip to content

Commit

Permalink
Added caching
Browse files Browse the repository at this point in the history
Also fixed an issue with multiple tweetsie instances not working together
  • Loading branch information
iamphill committed Feb 25, 2015
1 parent 049f541 commit fc5d507
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tweetsie",
"version": "0.3.1",
"version": "0.4.0",
"homepage": "https://github.com/iamphill/Tweetsie",
"description": "Twitter plugin built with JS! 'cause I mean why the hello not?!",
"main": "dist/Tweetsie.js",
Expand Down
57 changes: 54 additions & 3 deletions dist/Tweetsie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Tweetsie - Twitter plugin built with JS! 'cause I mean why the hello not?!
* @version v0.3.0
* @version v0.4.0
* @author Phil Hughes <[email protected]>
* @license MIT
*/
Expand Down Expand Up @@ -51,6 +51,9 @@ var Tweetsie = (function () {
return;
}

// Get the current cache and display
_this.getCurrentCache();

// Initialize request
_this.initRequest();
});
Expand All @@ -73,13 +76,58 @@ var Tweetsie = (function () {
writable: true,
configurable: true
},
getCurrentCache: {

/**
Get the current value from the cache and then render the body
**/

value: function getCurrentCache() {
if (typeof localStorage !== "undefined") {
var bodystring = localStorage.getItem("tweetsie-" + this.opts.widgetid);

if (bodystring !== null && bodystring !== "") {
// Parse the body
this.parseBody(bodystring);

// Testing method
if (this.opts.__cacheGet !== undefined) {
this.opts.__cacheGet();
}
}
}
},
writable: true,
configurable: true
},
saveCache: {

/**
Store a JSON object into localstorage as a string
@param String returned body string
**/

value: function saveCache(body) {
if (typeof localStorage !== "undefined") {
if (body !== "") {
localStorage.setItem("tweetsie-" + this.opts.widgetid, body);
}
}
},
writable: true,
configurable: true
},
initRequest: {

/**
Initialize all the data required for the request
**/

value: function initRequest() {
// Create a random callback name
// Based on const
this.callbackname = "" + TWITTER_CALLBACK_FUNCTION_NAME + "" + Math.floor(Math.random() * 2000000000);

// Create the Twitter URL
this.generateUrl();

Expand Down Expand Up @@ -116,7 +164,7 @@ var Tweetsie = (function () {
**/

value: function generateUrl() {
this.url = "https://cdn.syndication.twimg.com/widgets/timelines/" + this.opts.widgetid + "?suppress_response_codes=true&callback=" + TWITTER_CALLBACK_FUNCTION_NAME;
this.url = "https://cdn.syndication.twimg.com/widgets/timelines/" + this.opts.widgetid + "?suppress_response_codes=true&callback=" + this.callbackname;
},
writable: true,
configurable: true
Expand All @@ -130,11 +178,14 @@ var Tweetsie = (function () {
value: function requestCallback() {
var _this = this;

window[TWITTER_CALLBACK_FUNCTION_NAME] = function (d) {
window["" + this.callbackname] = function (d) {
// Is this request a 404?
if (d.headers !== undefined && d.headers.status === 404) {
_this.notFoundError(d.headers.message);
} else {
// Store in cache
_this.saveCache(d.body);

_this.parseBody(d.body);
}
};
Expand Down
4 changes: 2 additions & 2 deletions dist/Tweetsie.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc5d507

Please sign in to comment.