awesome_nested_fields 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -59,7 +59,7 @@ The next step is set up the form view with the `nested_fields_for` method. It re
|
|
59
59
|
<% # person fields... %>
|
60
60
|
|
61
61
|
<h2>Phones</h2>
|
62
|
-
<div class="
|
62
|
+
<div class="items">
|
63
63
|
<%= f.nested_fields_for :phones do |f| %>
|
64
64
|
<fieldset class="item">
|
65
65
|
<%= f.label :number %>
|
@@ -79,7 +79,7 @@ The next step is set up the form view with the `nested_fields_for` method. It re
|
|
79
79
|
|
80
80
|
The `nested_fields_for` method lists the phones this person has and also adds an empty template to the page for creating new phones. (Actually, there is too much code inside the block. If you're not working with a simple example like this you better extract this code into a partial and call just `render :phones` inside the block. Good coding practices, you know.)
|
81
81
|
|
82
|
-
If you're paying attention, you noticed the key elements are marked with special class names. We *need* this for the javascript code, so it knows what to do with each HTML element: the one that have the children must have the class `
|
82
|
+
If you're paying attention, you noticed the key elements are marked with special class names. We *need* this for the javascript code, so it knows what to do with each HTML element: the one that have the children must have the class `items`; each child must be marked with the class `item`; inside an item, the link for removal must have the class `remove`; and the link to add new items must have the class `add`. We can change the names later, but these are the default choices. Finally, don't forget to add the `id` field, as it is needed by AR to identify whether this is an existing or a new element, and the `_destroy` field to activate deletion when the user clicks on the remove link.
|
83
83
|
|
84
84
|
### Javascript
|
85
85
|
|
@@ -135,7 +135,7 @@ Keep in mind that you can call the templates only after `nested_fields_for` and
|
|
135
135
|
To make nested fields work dynamically, the JS code needs to know what elements to use. By default, this is made by marking key elements with CSS classes, but you can use other selectors (any valid jQuery selector will do). The available options are shown below.
|
136
136
|
|
137
137
|
* `itemSelector` marks each item from the collection (`.item` by default)
|
138
|
-
* `containerSelector` marks the element that contains the items (`.container` by default)
|
138
|
+
* `containerSelector` marks the element that contains the items (`.items` by default). You can also use `.container` by default, but beware this class name conflicts with many CSS frameworks (Blueprint, 960.gs, Bootstrap, Foundation, ...)
|
139
139
|
* `addSelector` marks the element that will add a new item to the container when clicked (`.add` by default)
|
140
140
|
* `removeSelector` marks the element inside an item that will remove it when clicked (`.remove` by default)
|
141
141
|
* `emptySelector` marks the element that is shown when there are no items; used in conjunction with `show_empty` option (`.empty` by default)
|
@@ -200,7 +200,7 @@ It is easy to have multiple nested fields on the same page. Instead of applying
|
|
200
200
|
<!-- ERB Code -->
|
201
201
|
<h2>Phones</h2>
|
202
202
|
<div id="phones">
|
203
|
-
<div class="
|
203
|
+
<div class="items">
|
204
204
|
<%= f.nested_fields_for :phones do |f| %>
|
205
205
|
<% ... %>
|
206
206
|
<% end %>
|
@@ -210,7 +210,7 @@ It is easy to have multiple nested fields on the same page. Instead of applying
|
|
210
210
|
|
211
211
|
<h2>Addresses</h2>
|
212
212
|
<div id="addresses">
|
213
|
-
<div class="
|
213
|
+
<div class="items">
|
214
214
|
<%= f.nested_fields_for :addresses do |f| %>
|
215
215
|
<% ... %>
|
216
216
|
<% end %>
|
@@ -242,6 +242,7 @@ TODO
|
|
242
242
|
* Make sure it can degrade gracefully
|
243
243
|
* Implement jQuery autoload
|
244
244
|
* Make `nested_fields_for` works without a block (looking for partials)
|
245
|
+
* Document how to nest nested fields (yeah, you can do that) ;-)
|
245
246
|
|
246
247
|
|
247
248
|
Copyleft
|
data/lib/rails/form_helper.rb
CHANGED
@@ -12,6 +12,7 @@ ActionView::Helpers::FormBuilder.class_eval do
|
|
12
12
|
options[:escape_template] = options.key?(:escape_template) ? options[:escape_template] : true
|
13
13
|
|
14
14
|
output = @template.capture { fields_for(association, &block) }
|
15
|
+
output ||= template.raw ""
|
15
16
|
|
16
17
|
if options[:show_empty] and self.object.send(association).empty?
|
17
18
|
output.safe_concat @template.capture { yield nil }
|
@@ -15,7 +15,7 @@
|
|
15
15
|
afterRemove: function(item) {},
|
16
16
|
itemTemplateSelector: '.item.template',
|
17
17
|
emptyTemplateSelector: '.empty.template',
|
18
|
-
containerSelector: '.container',
|
18
|
+
containerSelector: '.items, .container',
|
19
19
|
itemSelector: '.item',
|
20
20
|
emptySelector: '.empty',
|
21
21
|
addSelector: '.add',
|
@@ -29,7 +29,7 @@
|
|
29
29
|
init: function(options) {
|
30
30
|
return this.each(function() {
|
31
31
|
var $this = $(this);
|
32
|
-
if($
|
32
|
+
if($this.data('nested-fields.options')) {
|
33
33
|
log('Nested fields already defined for this element. If you want to redefine options, destroy it and init again.');
|
34
34
|
return $this;
|
35
35
|
}
|
@@ -42,7 +42,7 @@
|
|
42
42
|
$this.data('nested-fields.options', options);
|
43
43
|
|
44
44
|
bindInsertToAdd(options);
|
45
|
-
bindRemoveToItems(options);
|
45
|
+
bindRemoveToItems(options, $this);
|
46
46
|
});
|
47
47
|
},
|
48
48
|
|
@@ -105,8 +105,8 @@
|
|
105
105
|
});
|
106
106
|
}
|
107
107
|
|
108
|
-
function bindRemoveToItems(options) {
|
109
|
-
$(options.itemSelector,
|
108
|
+
function bindRemoveToItems(options, $this) {
|
109
|
+
$(options.itemSelector, $this).each(function(i, item) {
|
110
110
|
bindRemoveToItem(item, options);
|
111
111
|
});
|
112
112
|
}
|
@@ -178,7 +178,7 @@
|
|
178
178
|
if(!options.skipBefore) {
|
179
179
|
options.beforeRemove($element, remove);
|
180
180
|
if(options.beforeRemove.length <= 1) {
|
181
|
-
|
181
|
+
remove();
|
182
182
|
}
|
183
183
|
} else {
|
184
184
|
remove();
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_nested_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-12-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70151785785320 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70151785785320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &70151785784480 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 3.0.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70151785784480
|
36
36
|
description: Awesome dynamic nested fields for Rails and jQuery
|
37
37
|
email: lailson@guava.com.br
|
38
38
|
executables: []
|