-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit resource likes #87
base: master
Are you sure you want to change the base?
Changes from 6 commits
a36fd9f
52d23c9
75fe78a
1960c32
2a368b4
6b0b3e5
911663c
94e617b
936135d
5957b98
d6a2c34
a62eec8
eda5ad3
41da8b0
902dae5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,55 @@ function scrollToTop(event) { | |
$('html, body').animate({scrollTop: 0}, 'slow'); | ||
} | ||
|
||
function getCookie(cname) { | ||
var name = cname + "="; | ||
var ca = document.cookie.split(';'); | ||
for(var i = 0; i <ca.length; i++) { | ||
var c = ca[i]; | ||
while (c.charAt(0)==' ') { | ||
c = c.substring(1); | ||
} | ||
if (c.indexOf(name) == 0) { | ||
return c.substring(name.length,c.length); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
function setCookie(cname, cvalue, exdays) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here - thanks! |
||
var d = new Date(); | ||
d.setTime(d.getTime() + (exdays*24*60*60*1000)); | ||
var expires = "expires="+d.toUTCString(); | ||
document.cookie = cname + "=" + cvalue + "; " + expires; | ||
} | ||
|
||
function processLike(event, target){ | ||
event.preventDefault(); | ||
resourceID = target.parentNode.parentNode.action.split('/')[4] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way you rely on the absolute URL containing 5 forward slashes ('/'), which is not a good idea. That code would break if we change our URL paths. What you know (and what is independent of the rest of the path) is that the URL ends on '<resource_id>/like'. So instead of getting the 5th element of the array, I would use 'target.parentNode.parentNode.action.split('/').slice(-2)[0]'. That way you first reduce the array to the two last elements, and then choose the resource_id. |
||
|
||
//create shescoding_likes cookie if it does not exist | ||
if (getCookie("_shescoding_likes") === ""){ | ||
document.cookie = "_shescoding_likes = {}"; | ||
newValue = JSON.stringify({[resourceID]: true}); | ||
setCookie("_shescoding_likes", newValue, 365); | ||
incrementLike(event, target); | ||
} | ||
|
||
//if shescoding_likes cookie does exist | ||
else { | ||
//check if resource id exists | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure indentations in this function are right - e.g., everything in this else should be indented further. |
||
var oldCookie = getCookie("_shescoding_likes"); | ||
var newCookie = JSON.parse(oldCookie); | ||
|
||
if (!(newCookie.hasOwnProperty(resourceID))){ | ||
newCookie[resourceID] = true; | ||
newValue = JSON.stringify(newCookie); | ||
setCookie("_shescoding_likes", newValue, 365); | ||
incrementLike(event, target); | ||
}; | ||
}; | ||
}; | ||
|
||
function incrementLike(event, target) { | ||
event.preventDefault(); | ||
var form = $(target).parents('form'); | ||
|
@@ -45,4 +94,5 @@ function incrementLike(event, target) { | |
$.ajax(form.attr("action"), { | ||
type: "POST" | ||
}); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment here saying that this code is copied from W3 Schools, with the link: http://www.w3schools.com/js/js_cookies.asp