From 51605decff52c3e3fd39a99ed05abbc77f5b454d Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Tue, 24 Feb 2015 12:30:40 +0000 Subject: [PATCH] Changed callback functions to promises --- README.md | 11 ++--- bower.json | 2 +- dist/Tweetsie.js | 51 ++++++++++++---------- example/index.html | 7 +-- lib/Tweetsie.js | 45 +++++++++++--------- package.json | 2 +- tests/test.js | 103 ++++++++++++++++++++------------------------- 7 files changed, 109 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index af1f43c..a6704cf 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,11 @@ new Tweetsie({
\

{{body}}

\
\ - ', - callback: function (tweets) { + ' +}).then(function (tweets) { + +}).catch(function (error) { - } }); ``` @@ -49,8 +50,8 @@ The above creates a new instance of Tweetsie with all available options being us | widgetid | ID of the widget created on [Twitter](https://twitter.com/settings/widgets) | | count | Number of Tweets to parse. Defaults to all which is 20 | | template | String template used to display on the page | -| callback | This is called when Tweetsie has completed parsing all the tweets | -| error | Function that is called when any errors occur | + +Tweetsie uses ES6 Promises to be super awesome and hip! For older browsers, you will need to use a [Polyfill](https://github.com/jakearchibald/es6-promise). ## Styling diff --git a/bower.json b/bower.json index fd73c9d..22a7013 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "Tweetsie", - "version": "0.1.0", + "version": "0.2.0", "homepage": "https://github.com/iamphill/Tweetsie", "description": "Twitter plugin built with JS! 'cause I mean why the hello not?!", "main": "dist/Tweetsie.js", diff --git a/dist/Tweetsie.js b/dist/Tweetsie.js index 5b476fe..b735984 100644 --- a/dist/Tweetsie.js +++ b/dist/Tweetsie.js @@ -21,24 +21,31 @@ var Tweetsie = (function () { **/ function Tweetsie(opts) { + var _this = this; + _classCallCheck(this, Tweetsie); this.opts = opts; - // No opts error message - if (this.opts === undefined) { - this.error("No object was passed to Tweetsie"); - return; - } - - // Not widgetid error message - if (this.opts.widgetid === undefined) { - this.error("Must pass in a `widgetid`"); - return; - } - - // Initialize request - this.initRequest(); + return new Promise(function (resolve, reject) { + _this.resolve = resolve; + _this.reject = reject; + + // No opts error message + if (_this.opts === undefined) { + _this.error("No object was passed to Tweetsie"); + return; + } + + // Not widgetid error message + if (_this.opts.widgetid === undefined) { + _this.error("Must pass in a `widgetid`"); + return; + } + + // Initialize request + _this.initRequest(); + }); } _prototypeProperties(Tweetsie, null, { @@ -50,11 +57,9 @@ var Tweetsie = (function () { **/ value: function error(message) { - console.log("Tweetsie: " + message); - // Return error message to error callback - if (this.opts.error !== undefined) { - this.opts.error(message); + if (this.reject !== undefined) { + this.reject(message); } }, writable: true, @@ -158,15 +163,15 @@ var Tweetsie = (function () { // Loop all the tweets this.loopTweets(); - // Do the callback! - if (this.opts.callback !== undefined) { - this.opts.callback(this.tweets); - } - // Should Tweetsie parse the template and then create elements based on that? if (this.opts.template !== undefined) { this.parseTemplate(); } + + // Do the callback! + if (this.resolve !== undefined) { + this.resolve(this.tweets); + } }, writable: true, configurable: true diff --git a/example/index.html b/example/index.html index 1cd297a..f1d6801 100644 --- a/example/index.html +++ b/example/index.html @@ -21,10 +21,11 @@
\

{{body}}

\
\ - ', - callback: function (tweets) { + ' + }).then(function (tweets) { - } + }).catch(function (error) { + }); diff --git a/lib/Tweetsie.js b/lib/Tweetsie.js index 7bea654..b344961 100644 --- a/lib/Tweetsie.js +++ b/lib/Tweetsie.js @@ -16,20 +16,25 @@ class Tweetsie { constructor(opts) { this.opts = opts; - // No opts error message - if (this.opts === undefined) { - this.error('No object was passed to Tweetsie'); - return; - } + return new Promise((resolve, reject) => { + this.resolve = resolve; + this.reject = reject; + + // No opts error message + if (this.opts === undefined) { + this.error('No object was passed to Tweetsie'); + return; + } - // Not widgetid error message - if (this.opts.widgetid === undefined) { - this.error('Must pass in a `widgetid`'); - return; - } + // Not widgetid error message + if (this.opts.widgetid === undefined) { + this.error('Must pass in a `widgetid`'); + return; + } - // Initialize request - this.initRequest(); + // Initialize request + this.initRequest(); + }); } /** @@ -37,11 +42,9 @@ class Tweetsie { @param String message to output **/ error(message) { - console.log(`Tweetsie: ${message}`); - // Return error message to error callback - if (this.opts.error !== undefined) { - this.opts.error(message); + if (this.reject !== undefined) { + this.reject(message); } } @@ -115,15 +118,15 @@ class Tweetsie { // Loop all the tweets this.loopTweets(); - // Do the callback! - if (this.opts.callback !== undefined) { - this.opts.callback(this.tweets); - } - // Should Tweetsie parse the template and then create elements based on that? if (this.opts.template !== undefined) { this.parseTemplate(); } + + // Do the callback! + if (this.resolve !== undefined) { + this.resolve(this.tweets); + } } /** diff --git a/package.json b/package.json index f74d6b1..a782ece 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "Tweetsie", - "version": "0.1.0", + "version": "0.2.0", "description": "", "scripts": { "test": "gulp test" diff --git a/tests/test.js b/tests/test.js index 9682ad9..5977c45 100644 --- a/tests/test.js +++ b/tests/test.js @@ -11,11 +11,10 @@ // Create Tweetsie widget new Tweetsie({ - widgetid: widgetid, - callback: function (tweets) { - assert.equal(tweets.length, 20, 'Tweets not found!'); - done(); - } + widgetid: widgetid + }).then(function (tweets) { + assert.equal(tweets.length, 20, 'Tweets not found!'); + done(); }); }); @@ -24,10 +23,9 @@ new Tweetsie({ widgetid: '1', - error: function (msg) { - assert.equal(msg, NOT_FOUND_MSG, 'No error getting Tweets. Huh?'); - done(); - } + }).catch(function (msg) { + assert.equal(msg, NOT_FOUND_MSG, 'No error getting Tweets. Huh?'); + done(); }); }); @@ -35,10 +33,10 @@ var done = assert.async(); new Tweetsie({ - error: function (msg) { - assert.equal(msg, WIDGETID_ERROR_MSG, 'No widget ID'); - done(); - } + count: 1 + }).catch(function (msg) { + assert.equal(msg, WIDGETID_ERROR_MSG, 'No widget ID'); + done(); }); }); @@ -47,11 +45,10 @@ new Tweetsie({ widgetid: widgetid, - count: 1, - callback: function (tweets) { - assert.equal(tweets.length, 1, 'Tweets not found!'); - done(); - } + count: 1 + }).then(function (tweets) { + assert.equal(tweets.length, 1, 'Tweets not found!'); + done(); }); }); @@ -61,24 +58,23 @@ new Tweetsie({ widgetid: widgetid, - count: 1, - callback: function (tweets) { - var tweet = tweets[0]; - - assert.notEqual(tweet.id, undefined, 'Tweet ID not undefined'); - assert.notEqual(tweet.tweet_url, undefined, 'Tweet URL not undefined'); - assert.notEqual(tweet.date, undefined, 'Tweet date not undefined'); - assert.notEqual(tweet.author.avatar, undefined, 'Tweet avatar not undefined'); - assert.notEqual(tweet.author.name, undefined, 'Tweet name not undefined'); - assert.notEqual(tweet.author.profile_url, undefined, 'Tweet profile URL not undefined'); - assert.notEqual(tweet.author.username, undefined, 'Tweet username not undefined'); - assert.notEqual(tweet.actions.favorite, undefined, 'Tweet favorite URL not undefined'); - assert.notEqual(tweet.actions.reply, undefined, 'Tweet reply URL not undefined'); - assert.notEqual(tweet.actions.retweet, undefined, 'Tweet retweet URL not undefined'); - - // Complete! - done(); - } + count: 1 + }).then(function (tweets) { + var tweet = tweets[0]; + + assert.notEqual(tweet.id, undefined, 'Tweet ID not undefined'); + assert.notEqual(tweet.tweet_url, undefined, 'Tweet URL not undefined'); + assert.notEqual(tweet.date, undefined, 'Tweet date not undefined'); + assert.notEqual(tweet.author.avatar, undefined, 'Tweet avatar not undefined'); + assert.notEqual(tweet.author.name, undefined, 'Tweet name not undefined'); + assert.notEqual(tweet.author.profile_url, undefined, 'Tweet profile URL not undefined'); + assert.notEqual(tweet.author.username, undefined, 'Tweet username not undefined'); + assert.notEqual(tweet.actions.favorite, undefined, 'Tweet favorite URL not undefined'); + assert.notEqual(tweet.actions.reply, undefined, 'Tweet reply URL not undefined'); + assert.notEqual(tweet.actions.retweet, undefined, 'Tweet retweet URL not undefined'); + + // Complete! + done(); }); }); @@ -88,11 +84,10 @@ new Tweetsie({ widgetid: widgetid, count: 1, - template: '', - error: function (msg) { - assert.equal(msg, TEMPLATE_EMPTY_MATCHES, 'Template matches found?!'); - done(); - } + template: '' + }).catch(function (msg) { + assert.equal(msg, TEMPLATE_EMPTY_MATCHES, 'Template matches found?!'); + done(); }); }); @@ -102,33 +97,25 @@ new Tweetsie({ widgetid: widgetid, count: 1, - template: '{{ body }}', - error: function (msg) { - assert.equal(msg, NO_CONTAINER, 'Somehow found a container?'); - done(); - } + template: '{{ body }}' + }).catch(function (msg) { + assert.equal(msg, NO_CONTAINER, 'Somehow found a container?'); + done(); }); }); test('Tweet template with container', function (assert) { var done = assert.async(); - var i = 0; new Tweetsie({ container: 'tweetsie-container', widgetid: widgetid, count: 1, - template: '

{{ body }}

', - callback: function (tweets) { - if (i === 0) { - setTimeout(function () { - var el = document.getElementById(CONTAINER_ID); - assert.equal(el.getElementsByTagName('p').length, 1, 'Template didn\'t render right!'); - done(); - }, 200); - } - i++; - } + template: '

{{ body }}

' + }).then(function (tweets) { + var el = document.getElementById(CONTAINER_ID); + assert.equal(el.getElementsByTagName('p').length, 1, 'Template didn\'t render right!'); + done(); }); }); })();