Вопрос

I've searched exhaustively but can't find an answer for this.

I'm writing a small app using padrino and I have 2 views, person and event. I have views and controllers for these and they are working fine.

My issue is that from the 'event' view, I want to open a modal window to add a new person.

The modal window will load up with the relevant fields for a 'person'. Once the fields are filled and a button clicked, the modal window will disappear, the new 'person' will be saved, and the user will be back on the event page.

Could anyone give me advice on how to proceed ? Thank you.

Это было полезно?

Решение

A modal window is generally done using javascript. For example, using jQuery UI (from the docs)

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Dialog - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
  $(function() {
    $( "#dialog" ).dialog();
  });
  </script>
</head>
<body>

<div id="dialog" title="Basic dialog">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>


</body>
</html>

Sinatra/Padrino will take care of the layout and the view, you just have to add the script portion. Assuming you're using Haml, your view would look like this:

#dialog{title: "Basic dialog"}
  %p
    This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.

:javascript
  $(function() {
    $( "#dialog" ).dialog();
  });

The javascript doesn't need to go inside the head tags as the jQuery docs suggest.


Edit:

There are a couple of choices for loading a partial into a window. One is to use a javascript library like Handlebars, but I prefer to get the javascript to load it from a route, that way all the view code stays together, e.g.

get "/templates/modal1/?" do
  haml :modal1, :layout => !xhr?
end

In the javascript, make an AJAX call to that route and you'll get only the HTML sans the layout, then use it to fill the box:

$.get('ajax/test.html', function(data) {
  $('#dialog').html(data);
}

and in your view just describe the div:

#dialog1
  -# The HTML from the view will be put here by the jQuery code.

Something like that.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top