Created
January 31, 2012 03:35
-
-
Save brianyang/1708590 to your computer and use it in GitHub Desktop.
plugin architecture guide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.likeButton = function() { | |
var obj = this; | |
var liked; | |
var status = obj.attr('class'); | |
if (status && status.indexOf('active') != -1) { | |
liked = true; | |
} else { | |
liked = false; | |
} | |
function postLiked(id) { | |
$.post('/hotel/like/', | |
{ | |
'hotel_id': id | |
}, | |
function(data) { | |
}); | |
} | |
obj.click(function() { | |
if (liked == false) { | |
if (isLoggedIn == false) { | |
$.fn.lightbox.closeBox(); | |
$.fn.lightbox.show('#facebook-sign-in'); | |
} else { | |
//url = window.location.pathname; | |
id = $(this).attr('id'); | |
$(this).addClass('active'); | |
postLiked(id); | |
liked = true; | |
} | |
} else { | |
$(this).removeClass('active'); | |
id = $(this).attr('id'); | |
postLiked(id); | |
liked = false; | |
} | |
}); | |
}; | |
})(jQuery); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.likeButton = function(){ | |
var obj = this, liked, status = obj.attr('class') | |
status && status.indexOf('active') !!== -1 ? liked = true :liked = false | |
var postLiked = function(id){ | |
$.post('/hotel/like/', | |
{ | |
'hotel_id': id | |
}) | |
obj.click(function(){ | |
liked !=== false ? notLiked() : signInFirst() | |
var signInFIrst = function(){ | |
$.fn.lightbox.closeBox().show('#facebook-sign-in'); | |
}, | |
alreadyLoggedIn = function(){ | |
id = $(this).attr('id'); | |
$(this).addClass('active'); | |
postLiked(id); | |
liked = true; | |
}, | |
notLiked = function(){ | |
$(this).removeClass('active'); | |
id = $(this).attr('id'); | |
postLiked(id); | |
liked = false; | |
} | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GLOBAL_LIB.likeButton = function(){ | |
var obj = this, liked, status = obj.attr('class') | |
status && status.indexOf('active') !!== -1 ? liked = true :liked = false | |
var postLiked = function(id){ | |
$.post('/hotel/like/', | |
{ | |
'hotel_id': id | |
}) | |
obj.click(function(){ | |
liked !=== false ? notLiked() : signInFirst() | |
var signInFIrst = function(){ | |
$.fn.lightbox.closeBox().show('#facebook-sign-in'); | |
}, | |
alreadyLoggedIn = function(){ | |
id = $(this).attr('id'); | |
$(this).addClass('active'); | |
postLiked(id); | |
liked = true; | |
}, | |
notLiked = function(){ | |
$(this).removeClass('active'); | |
id = $(this).attr('id'); | |
postLiked(id); | |
liked = false; | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this plugin lets the user choose to like or unlike, then adds a class and renders the lightbox. this can also be coded using function statements instead of function expressions. the final example illustrates namespacing the object a custom library and not cluttering the jQuery namespace.