Skip to content

Instantly share code, notes, and snippets.

@wojtha
Forked from coreyward/README.md
Created December 5, 2012 18:39
Show Gist options
  • Select an option

  • Save wojtha/4218293 to your computer and use it in GitHub Desktop.

Select an option

Save wojtha/4218293 to your computer and use it in GitHub Desktop.

Revisions

  1. @coreyward coreyward revised this gist Nov 9, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions modals.js.coffee
    Original file line number Diff line number Diff line change
    @@ -7,15 +7,15 @@ $ ->
    $modal_container = $('#modal-container')

    # Handle modal links with the data-remote attribute
    $.on 'ajax:success', 'a[data-remote]', (xhr, data, status) ->
    $(document).on 'ajax:success', 'a[data-remote]', (xhr, data, status) ->
    $modal
    .html(data)
    .prepend($modal_close)
    .css('top', $(window).scrollTop() + 40)
    .show()
    $modal_container.show();

    $.on 'click', '#modal .close', ->
    $(document).on 'click', '#modal .close', ->
    $modal_container.hide()
    $modal.hide()
    false
  2. @coreyward coreyward revised this gist Dec 10, 2011. 2 changed files with 1 addition and 2 deletions.
    2 changes: 0 additions & 2 deletions _modal.html.erb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    # in a view, perhaps a _modal.html.erb partial that is included into
    # your layout (probably at the bottom).
    <div id="modal-container"></div>
    <div id="modal">
    <a href="#" class="close">close</a>
    1 change: 1 addition & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # in your app controller, you'll want to set the layout to nil for XHR (ajax) requests

    class ApplicationController < ActionController::Base
    layout Proc.new { |controller| controller.request.xhr? ? nil : 'application' }
    end
  3. @coreyward coreyward created this gist Dec 10, 2011.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    This is a Rails 3.1 adaptation of an answer to a StackOverflow question regarding using jQuery to display AJAX-retrieved content in a modal. Here is the original answer:

    http://stackoverflow.com/questions/5766055/jquery-modal-windows-and-edit-object/5766232#5766232
    6 changes: 6 additions & 0 deletions _modal.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # in a view, perhaps a _modal.html.erb partial that is included into
    # your layout (probably at the bottom).
    <div id="modal-container"></div>
    <div id="modal">
    <a href="#" class="close">close</a>
    </div>
    33 changes: 33 additions & 0 deletions _modal.scss
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // This partial should be @imported into your layout Sass file, or included via the Asset Pipeline
    //

    #modal-container {
    display: none;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,0.4);
    }

    #modal {
    display: none;
    position: absolute;
    width: 600px;
    left: 50%;
    margin-left: (-600px - 40px) / 2;
    padding: 20px;
    background: #fff;
    border: 5px solid #eee;

    & > .close {
    position: absolute;
    right: 5px;
    top: 5px;
    color: #666;
    &:hover, &:active {
    color: #000;
    }
    }
    }
    3 changes: 3 additions & 0 deletions application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    //= jquery
    //= jquery_ujs
    //= modals
    4 changes: 4 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # in your app controller, you'll want to set the layout to nil for XHR (ajax) requests
    class ApplicationController < ActionController::Base
    layout Proc.new { |controller| controller.request.xhr? ? nil : 'application' }
    end
    1 change: 1 addition & 0 deletions index.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <%= link_to 'New Post', new_post_path, :remote => true %>
    21 changes: 21 additions & 0 deletions modals.js.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # This file should be "required" into your `application.js` via the Asset Pipeline.
    #

    $ ->
    $modal = $('#modal')
    $modal_close = $modal.find('.close')
    $modal_container = $('#modal-container')

    # Handle modal links with the data-remote attribute
    $.on 'ajax:success', 'a[data-remote]', (xhr, data, status) ->
    $modal
    .html(data)
    .prepend($modal_close)
    .css('top', $(window).scrollTop() + 40)
    .show()
    $modal_container.show();

    $.on 'click', '#modal .close', ->
    $modal_container.hide()
    $modal.hide()
    false