SrBuj 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +42 -17
- data/lib/SrBuj/version.rb +1 -1
- data/lib/assets/javascripts/SrBuj.js +68 -28
- metadata +8 -8
data/README.md
CHANGED
@@ -3,24 +3,41 @@ SrBuj
|
|
3
3
|
|
4
4
|
Better Unobtrusive JavaScript Request (for Rails and jquery_ujs, twitter/bootstrap modal.js)
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
SrBuj comes to cure a common illness in a life of every Rails developer (maybe other kind too):
|
7
|
+
- write the same code to rise up a modal
|
8
|
+
- render a partial on it.
|
9
|
+
- change an element in a view without reload the entire web page
|
10
|
+
- replace content of an element (GET)
|
11
|
+
- add an element (POST)
|
12
|
+
- remove an element (DELETE)
|
13
|
+
- replace an specific element (PUT/PATCH)
|
14
|
+
- bind a callback to run specific js function.
|
15
|
+
|
16
|
+
every time, we end up with a lot of code, ugly code, messy code...
|
17
|
+
|
18
|
+
And every time we end up doing an old and known .js.rb file, with the same 4 lines... Well enough it's enough!
|
19
|
+
|
20
|
+
This is the cure... and comes with a simple treatment too!
|
21
|
+
Steps:
|
22
|
+
1. Bundle the gem
|
23
|
+
2. required in your manifest
|
24
|
+
3. add the magic data attributes to the element
|
25
|
+
4. Enjoy de js-less ! :-)
|
10
26
|
|
11
27
|
This unobtrusive scripting support file is developed for the Ruby on Rails framework, but is not strictly tied to any specific backend. You can drop this into any application to:
|
12
28
|
|
13
29
|
- get modals out of the box, via js
|
14
30
|
- make non-GET requests from hyperlinks
|
15
|
-
- make
|
31
|
+
- make any submit data asynchronously with Ajax and handle the response in a dry way.
|
16
32
|
|
17
33
|
These features are achieved by adding certain ["data" attributes][data] to your HTML markup. In Rails, they are added by the framework's template helpers.
|
18
34
|
|
19
35
|
Requirements
|
20
36
|
------------
|
21
37
|
|
22
|
-
- [jQuery 1.7.x or higher]
|
23
|
-
- [
|
38
|
+
- [jQuery 1.7.x or higher](http://jquery.com/);
|
39
|
+
- [jquery_ujs](https://github.com/rails/jquery-ujs)
|
40
|
+
- [twitter/bootstrap modal.js plugin](http://twitter.github.com/bootstrap/javascript.html#modals) or any .modal()/.modal('toggle') function attached to the jquery element.
|
24
41
|
- HTML5 doctype (optional).
|
25
42
|
|
26
43
|
If you don't use HTML5, adding "data" attributes to your HTML4 or XHTML pages might make them fail [W3C markup validation][validator]. However, this shouldn't create any issues for web browsers or other user agents.
|
@@ -48,19 +65,26 @@ a. For Rails 3.1, add these lines to the top of your app/assets/javascripts/appl
|
|
48
65
|
//= require SrBuj
|
49
66
|
```
|
50
67
|
|
51
|
-
Use
|
68
|
+
Use and Options
|
52
69
|
---
|
53
|
-
data[target]
|
54
|
-
data[modal]
|
55
|
-
data[error]
|
56
|
-
data[replace]
|
57
|
-
data[
|
58
|
-
|
70
|
+
- target( data[target] ): depending on the type of request(GET/PUT/POST/DELETE) is used to alter the Dom. it represent the element that we want to alter in the view after an succceded request (needed)
|
71
|
+
- modal( data[modal] ): if you wish that the response ends up in a modal. value: true|false (default false)
|
72
|
+
- error (data[error]): id Element In a form data[error] is the holder in witch de form re renders to show the errors
|
73
|
+
- replace(data[replace]): if you wish to replace de data[target] element with the response content on success. represent the PUT/PATCH action for SrBuj. value: true|false (default false)
|
74
|
+
- remove (data[method:delete] && data[target]): if these are combined the success response execute a remove() over the data[target] element.
|
75
|
+
- nochange (data[nochange]): do all but don't change the document in any way.
|
76
|
+
- callback(data[callback]) = after a successeded request, call this function.
|
77
|
+
- data[custom] = just proxy the response to my custom function in callback, nothing more.
|
78
|
+
- jqueryselector(data[jqueryselector]): Change the data[target] & data[error] for selectors in jquery and find the element!
|
79
|
+
|
80
|
+
You can use it with any element available. (links forms tables divs anything).
|
59
81
|
|
60
82
|
Example
|
61
|
-
|
83
|
+
=======
|
62
84
|
|
63
|
-
|
85
|
+
modal
|
86
|
+
----
|
87
|
+
in your view (example with a link and haml)
|
64
88
|
|
65
89
|
````haml
|
66
90
|
= link_to 'add Element' elements_path, remote: true, data: {target: 'partial-id', modal: true}
|
@@ -75,7 +99,8 @@ in your controller
|
|
75
99
|
```` ruby
|
76
100
|
def new
|
77
101
|
@element= Element.new
|
78
|
-
render partial: 'new', content_type: 'text/html'
|
102
|
+
render partial: 'new', content_type: 'text/html' #=> the content_type stands for telling the js request that everything ends up
|
103
|
+
fine.
|
79
104
|
end
|
80
105
|
````
|
81
106
|
|
data/lib/SrBuj/version.rb
CHANGED
@@ -8,9 +8,26 @@
|
|
8
8
|
* Released under the MIT license
|
9
9
|
*
|
10
10
|
*/
|
11
|
+
$.fn.present = function(){
|
12
|
+
return this.length === 1 && this
|
13
|
+
};
|
11
14
|
var SrBuj;
|
12
15
|
$.SrBuj = SrBuj = {
|
13
16
|
selector: '[data-remote][data-target]',
|
17
|
+
defaults: {
|
18
|
+
'$el': undefined,
|
19
|
+
target: undefined,
|
20
|
+
onError: undefined,
|
21
|
+
wrapper: undefined,
|
22
|
+
method: 'GET',
|
23
|
+
modal: false,
|
24
|
+
custom: false,
|
25
|
+
change: true,
|
26
|
+
custom: false,
|
27
|
+
replace: false,
|
28
|
+
callback: false,
|
29
|
+
jqueryselector: false
|
30
|
+
},
|
14
31
|
changeDom: function(method,$target,data){
|
15
32
|
switch(method){
|
16
33
|
case 'POST':
|
@@ -30,42 +47,65 @@
|
|
30
47
|
},
|
31
48
|
getVerb: function($el){
|
32
49
|
var dataVerb = $el.data('method'),
|
33
|
-
replace = $el.data('replace')
|
34
|
-
proto = $el.attr('method')
|
50
|
+
replace = $el.data('replace'),
|
51
|
+
proto = $el.attr('method');
|
35
52
|
if( dataVerb )
|
36
|
-
return dataVerb.toUpperCase()
|
53
|
+
return dataVerb.toUpperCase();
|
37
54
|
else
|
38
|
-
|
55
|
+
if (proto) return replace ? 'PUT' : proto.toUpperCase();
|
56
|
+
},
|
57
|
+
getOptions: function(el, user_options){
|
58
|
+
var options = {},
|
59
|
+
$el = $( user_options['el'] || el ).present(),
|
60
|
+
el_options = {
|
61
|
+
'$el': $el,
|
62
|
+
'target': $el.data('target'),
|
63
|
+
'method': $.SrBuj.getVerb($el),
|
64
|
+
'onError': $el.data('error'),
|
65
|
+
'callback': $el.data('callback'),
|
66
|
+
'change': ! ( $el.data('nochange') || false ),
|
67
|
+
'jqueryselector': $el.data('jqueryselector'),
|
68
|
+
'modal': $el.data('modal'),
|
69
|
+
'wrapper': $el.data('error'),
|
70
|
+
'custom': $el.data('custom'),
|
71
|
+
'replace': $el.data('replace')
|
72
|
+
};
|
73
|
+
for(var attr in this.defaults){
|
74
|
+
options[attr] = user_options[attr] || el_options[attr] || this.defaults[attr];
|
75
|
+
}
|
76
|
+
return options;
|
39
77
|
},
|
40
|
-
success: function(e, data){
|
41
|
-
var
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
fn = $el.data('callback'),
|
46
|
-
custom = $el.data('custom') || false,
|
47
|
-
callback = (typeof fn == 'function') ? fn : window[fn],
|
48
|
-
target = document.getElementById($el.data('target')),
|
49
|
-
wrapper = document.getElementById($el.data('error')),
|
50
|
-
$target = $(target);
|
78
|
+
success: function(e, data,status,user_options){
|
79
|
+
var options = $.SrBuj.getOptions(e.target, user_options),
|
80
|
+
$el = options['$el'],
|
81
|
+
$target = options['jqueryselector'] ? $(options['target']).present() : $(document.getElementById(options['target'])) ,
|
82
|
+
$wrapper = options['jqueryselector'] ? $(options['wrapper']) : $(document.getElementById(options['wrapper']));
|
51
83
|
|
52
|
-
if(
|
53
|
-
if(change)
|
54
|
-
|
84
|
+
if(!options['custom']){
|
85
|
+
if(options['change'])
|
86
|
+
$.SrBuj.changeDom(options['method'],$target,data);
|
87
|
+
if(options['modal'])
|
88
|
+
options['wrapper'] ? $wrapper.modal('toggle') : $target.modal('toggle');
|
55
89
|
}
|
56
|
-
if(callback
|
90
|
+
if(options['callback'])
|
91
|
+
callback = (typeof options['callback'] == 'function') ? options['callback'] : window[options['callback']],
|
92
|
+
callback.apply(this,[e, data, status]);
|
57
93
|
},
|
58
|
-
fail: function(e, data){
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
94
|
+
fail: function(e, data, status, user_options){
|
95
|
+
var $el = $(e.target).present(),
|
96
|
+
error = $el.data('error'),
|
97
|
+
jqueryselector = $el.data('jqueryselector'),
|
98
|
+
$error = jqueryselector ? $(error) :$(document.getElementById(error));
|
99
|
+
if( error ){
|
100
|
+
$.SrBuj.changeDom('ERROR', $error, data.responseText);
|
101
|
+
}
|
63
102
|
else
|
64
|
-
throw 'cant find data-error
|
103
|
+
throw 'cant find data-error on element';
|
65
104
|
},
|
66
|
-
bind: function(){
|
67
|
-
|
68
|
-
$(document).on('ajax:
|
105
|
+
bind: function(selector){
|
106
|
+
var selector = selector || this.selector;
|
107
|
+
$(document).on('ajax:success', selector, this.success);
|
108
|
+
$(document).on('ajax:error', selector, this.fail);
|
69
109
|
}
|
70
110
|
}
|
71
111
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: SrBuj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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: 2013-03-
|
12
|
+
date: 2013-03-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70140467388800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '1.3'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70140467388800
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: railties
|
27
|
-
requirement: &
|
27
|
+
requirement: &70140467388300 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.1'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70140467388300
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70140467387880 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70140467387880
|
47
47
|
description: Better Unobtrusive JavaScript Request in asset pipeline
|
48
48
|
email:
|
49
49
|
- xeroice@gmail.com
|