Created
November 15, 2011 20:39
-
-
Save julioprotzek/1368267 to your computer and use it in GitHub Desktop.
Refactored Javascript
This file contains 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(){ | |
$('.star-rating').each(function(){ | |
$(this).raty({ | |
path: '/images/', | |
readOnly: cookieTest($(this).attr('id')), | |
start: 3, | |
click: function(score, evt) { | |
$.fn.raty.readOnly(true, '#' + this.attr('id') ); | |
$.cookie(this.attr('id'), 'yes', { expires: 7 }); | |
this.next().html("(32 Votes) <br> Thanks for voting!"); | |
//callback se der certo cria o cookie e thanks // else ele reativa o rating | |
// $.post("/credit-card/" + this.attr('id') + "/rate/" + score, function(data) { | |
// $.cookie(this.attr('id'), 'yes', { expires: 7 }); | |
// }); | |
} | |
}); | |
}); | |
function cookieTest(id) { | |
if($.cookie(id) == 'yes') { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
}); |
This file contains 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(){ | |
$('.star-rating').each(addStarRating); | |
}); | |
function addStarRating(){ | |
$(this).raty({ | |
path: '/images/', | |
readOnly: userAlreadyRated(this.id), | |
start: 3, | |
click: rate | |
}); | |
} | |
function userAlreadyRated(id) { | |
return $.cookie(id) == 'yes'; | |
} | |
function rate(score, evt) { | |
setAsReadOnly(this); | |
saveCookie(this); | |
thankYouMessage(this); | |
// saveInTheDatabase(this); | |
} | |
function saveCookie(creditCard) { | |
$.cookie(creditCard.attr('id'), 'yes', { expires: 7 }); | |
} | |
function setAsReadOnly(creditCard) { | |
$.fn.raty.readOnly(true, '#' + creditCard.attr('id') ); | |
} | |
function thankYouMessage(creditCard) { | |
creditCard.next().html("(35 Votes) <br> Thanks for voting!"); | |
} | |
function saveInTheDatabase(creditCard) { | |
$.post("/credit-card/" + creditCard.attr('id') + "/rate/" + score, success); | |
} | |
function success(data) { | |
$.cookie(this.attr('id'), 'yes', { expires: 7 }); | |
} |
This file contains 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
$ -> | |
$('.star-rating').each -> | |
new CreditCardStarRating(this) | |
class CreditCardStarRating | |
constructor: (@creditCard) -> | |
@id= @creditCard.id | |
$(@creditCard).raty | |
path: '/images/' | |
readOnly: @userAlreadyRated() | |
start: parseInt($(@creditCard).data('rating'), 10) | |
click: @rate | |
userAlreadyRated: => | |
$.cookie(@id) == 'yes' | |
rate: (@score, evt) => | |
@setAsReadOnly() | |
@saveCookie() | |
@thankYouMessage() | |
# @saveInTheDatabase() | |
saveCookie: => | |
$.cookie(@id, 'yes', { expires: 7 }) | |
setAsReadOnly: => | |
$.fn.raty.readOnly(true, '#' + @id) | |
thankYouMessage: => | |
$(@creditCard).next().html("(#{@numberOfVotes} Votes) <br> Thanks for voting!") | |
numberOfVotes: => | |
parseInt($(@creditCard).data('votes'), 10) + 1 | |
saveInTheDatabase: => | |
$.post("/credit-card/" + @id + "/rate/" + @score, @afterSave) | |
afterSave: (data) => | |
@saveCookie() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment