Skip to content

Instantly share code, notes, and snippets.

@dlynam
Created June 17, 2014 18:06
Show Gist options
  • Select an option

  • Save dlynam/702ec21d7a2fcc3086df to your computer and use it in GitHub Desktop.

Select an option

Save dlynam/702ec21d7a2fcc3086df to your computer and use it in GitHub Desktop.
<!--
The purpose of the following is to provide a callback to the parent window
so we can transfer the data retrieved from Gmail omnicontacts
-->
<%= hidden_field_tag 'contacts_callback', @contacts.to_json, id: "contacts_callback" %>
<% content_for :scripts do %>
<script>
// Set a Global variable to be accessed by the parent window
var contacts_list;
$(document).ready(function() {
// Pass the Rails hidden tag to our variable
contacts_list = $("#contacts_callback").val();
// Reference to the parent window if needed
if ( self.opener != null ) {
var parentWindow = self.opener;
}
// Close this popup, will allow our listening (on close) plugin to initiate the callback
window.close()
});
</script>
<% end %>
def contacts_callback
#other code
@contacts = request.env['omnicontacts.contacts']
# render contacts_callback.html.erb
render 'contacts_callback', :layout => 'application'
end
/*!
* jQuery OAuth via popup window plugin
*
* @author Nobu Funaki @nobuf
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($){
// inspired by DISQUS
$.oauthpopup = function(options)
{
if (!options || !options.path) {
throw new Error("options.path must not be empty");
}
options = $.extend({
windowName: 'ConnectWithOAuth' // should not include space for IE
, windowOptions: 'location=0,status=0,width=800,height=400'
, callback: function(){ window.location.reload(); }
}, options);
var oauthWindow = window.open(options.path, options.windowName, options.windowOptions);
var oauthInterval = window.setInterval(function(){
if (oauthWindow.closed) {
window.clearInterval(oauthInterval);
options.callback(oauthWindow);
}
}, 1000);
};
//bind to element and pop oauth when clicked
$.fn.oauthpopup = function(options) {
$this = $(this);
$this.click($.oauthpopup.bind(this, options));
};
})(jQuery);
// oAuth click event
//==================================
$(document).on('click','.oauth', function(event){
event.preventDefault();
var path = $(this).attr("href");
//create new oauthh popup window and monitor it
$.oauthpopup({
path: path,
windowOptions: 'width=800,height=650',
callback: function(childWindow) {
var contacts = $.parseJSON(childWindow.contacts_list);
contactsListView(contacts);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment