Created
January 20, 2011 23:29
-
-
Save gdakram/788932 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<title>3rd party website using the bookmarking widget</title> | |
</head> | |
<body> | |
....content.... | |
<!-- Embed the script tag to make the widget show here --> | |
<script src="http://bookmarking-site.com/javascripts/widget.js"></script> | |
....content.... | |
</body> | |
</html> |
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
class BookmarksController < ApplicationController | |
# The rails route file declaration to for save.json | |
# action to repond to GET request. | |
# | |
# resources :bookmarks do | |
# collection do | |
# get :save | |
# end | |
# end | |
# | |
def save | |
# if there's a user found | |
if current_user | |
Bookmark.create(:url => params[:url], :user => current_user) | |
response = "window.Academia.bookmark_callback(true);" | |
else | |
response = "window.Academia.bookmark_callback(false);" | |
end | |
respond_to do |wants| | |
wants.json { render :text => response } | |
end | |
end | |
private | |
def current_user | |
@current_user ||= User.find(session[:user_id]) if session[:user_id] | |
end | |
end |
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
// write the widget into the page | |
document.write('<a href="#" class="save_to_academia_bookmarks">Bookmark This Page</a>'); | |
window.Academia = window.Academia || {}; | |
(function(a){ | |
// This is the callback function the json response from the server | |
a.bookmark_callback = function(saved){ | |
if (!saved) { | |
console.log("page was not saved, respond appropriately... open up authentication window etc"); | |
} | |
else { | |
console.log("page was saved, respond appropriately... indicate to user it was saved"); | |
} | |
} | |
// the click event handler for bookmarking widget link | |
a.a_links = document.getElementsByClassName("save_to_academia_bookmarks"); | |
for (i = 0; i < a.a_links.length; i++) { | |
a.a_links[i].onclick = function(e){ | |
var script = document.createElement('script'); | |
script.src = "http://bookmarking-site.com/bookmarks/save.json?url=" + window.location.href; | |
document.body.appendChild(script); | |
return false; | |
} | |
} | |
})(Academia); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cross-domain Javascript using JSONP.