Trouble Converting jQuery Script to Plugin -
i'm trying convert pretty simple script takes search results twitter , outputs unordered list. first time trying write plugin , doesn't seem firing code when call it. script iteself works fine, code i've written plugin:
(function($) { $.fn.tweetget = function(options) { var defaults = { query: 'from:twitter&rpp=10', url: 'http://search.twitter.com/search.json?callback=?&q=' }; var options = $.extend(defaults, options); return this.each(function() { // tweets user query $.getjson(options.url + options.query, function(data) { var tweets = []; $.each(data.results, function(i, tweet) { tweets.push('<li>' + tweet.text.parseurl().parseusername().parsehashtag() + '</li>'); }); $('#target').append('<ul>' + tweets.join('') + '</ul>'); }); // parse tweets urls , convert links string.prototype.parseurl = function() { return this.replace(/[a-za-z]+:\/\/[a-za-z0-9-_]+\.[a-za-z0-9-_:%&~\?\/.=]+/g, function(url) { return url.link(url); }); }; // parse tweets twitter usernames , convert links string.prototype.parseusername = function() { return this.replace(/(?:^|\s)@[a-za-z0-9_.-]+\b/, function(user) { var username = user.replace("@","") return user.link("http://twitter.com/"+username); }); }; // parse tweets hashtags , convert links string.prototype.parsehashtag = function() { return this.replace(/[#]+[a-za-z0-9-_]+/g, function(hash) { var hashtag = hash.replace("#","%23") return hash.link("http://search.twitter.com/search?q="+hashtag); }); }; }); }; })(jquery);
the function being called with:
$('#target').tweetget({query: 'from:twitter&rpp:10'});
everything outside of return this.each(function() {}; working fine, nothing placed within firing or giving me errors. tutorials i've read seem use same basic format can't seem figure out i'm doing wrong...
here working version: http://jsfiddle.net/jaaulde/qk35d/3/
( function( global ) { var string, $; if( global.jquery ) { string = global.string; $ = window.jquery; string.prototype = $.extend( string.prototype, { // parse tweets urls , convert links parseurl: function() { return this.replace( /[a-za-z]+:\/\/[a-za-z0-9-_]+\.[a-za-z0-9-_:%&~\?\/.=]+/g, function( url ) { return url.link( url ); } ); }, // parse tweets twitter usernames , convert links parseusername: function() { return this.replace( /@[a-za-z0-9_.-]+\b/g, function( user ) { return user.link( 'http://twitter.com/' + user.replace( '@', '' ) ); } ); }, // parse tweets hashtags , convert links parsehashtag: function() { return this.replace( /[#]+[a-za-z0-9-_]+/g, function( hash ) { return hash.link( 'http://search.twitter.com/search?q=' + hash.replace( '#', '%23' ) ); } ); } } ); $.fn.tweetget = function( options ) { var defaults = { query: 'from:twitter&rpp=10', url: 'http://search.twitter.com/search.json?callback=?&q=' }; options = $.extend( defaults, options ); return this.each( function() { var target = this; // tweets user query $.getjson( options.url + options.query, function( data ) { var tweets = []; $.each( data.results, function( i, tweet ) { tweets.push( '<li>' + tweet.text.parseurl().parseusername().parsehashtag() + '</li>' ); } ); $( target ).append( '<ul>' + tweets.join('') + '</ul>' ); } ); } ); }; } }( window ) );
watch answer updates explain modifications.
edits:
- removed modifications of string prototype out of plugin--they should not there not part of plugin code, , redefined on every call plugin.
- removed
#target
selector within plugin targeted element rather using collection against plugin had been called (adjusted scope due getjson callback). - removed
var
declaration in front ofoptions = $.extend( defaults, options );
unneeded , there risk of wiping had been passed plugin on execution. - as aside, fixed
parseusername
function stop adding space in front of usernames in username urls - used preferred syntax localizing code
Comments
Post a Comment