angular_sprinkles 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/README.md +35 -0
- data/VERSION +1 -1
- data/angular_sprinkles.gemspec +2 -2
- data/app/assets/javascripts/angular_sprinkles.js.erb +18 -1
- data/lib/angular_sprinkles/helpers/bind_helper.rb +5 -0
- data/spec/helpers/bind_helper_spec.rb +74 -60
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82a8542cbe3370f7c7a63a7e6b535827297d427d
|
4
|
+
data.tar.gz: 7d47544aeb6a441e35705226756741c849176d12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98f6621a65033d9271a6ee611a7f8abaa046686f473c3b5af5cb238ca467247ad53a7569d4113f18e6fda93a79b25c1027d089b08d1407b9e918272bd7a79b22
|
7
|
+
data.tar.gz: 796d155b10422c2c6050dbf526afc110a6a0a1ff4319c58345909915305df22cad10c14dd11e7027ad192907bc085f6b7bcca55de01846fe0528c106661dc619
|
data/README.md
CHANGED
@@ -91,6 +91,20 @@ Last Name: <input type="text" ng-model="<%= @user.bind(:last_name) %>" />
|
|
91
91
|
|
92
92
|
We've got two-way data binding of persisted data without writing a single line of javascript!
|
93
93
|
|
94
|
+
## Binding Collections
|
95
|
+
|
96
|
+
You can bind collections with the `bindable_collection` helper
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
class UserController < ApplicationController
|
100
|
+
include AngularSprinkles::Controller
|
101
|
+
|
102
|
+
def show
|
103
|
+
@users = bindable_collection(User.all)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
94
108
|
## Custom Directives
|
95
109
|
|
96
110
|
Sprinkles also comes packaged with a helper for instantiating custom directives.
|
@@ -139,6 +153,27 @@ sprinkles.directive('blink', function ($interval) {
|
|
139
153
|
<input type="text" ng-model="<%= bind(:blink_text) %>" />
|
140
154
|
```
|
141
155
|
|
156
|
+
## Custom Functions
|
157
|
+
|
158
|
+
Custom functions can be added to your application with the `sprinkles.func` function
|
159
|
+
|
160
|
+
```js
|
161
|
+
// some js file
|
162
|
+
|
163
|
+
sprinkles.func('alertMe', function (input) {
|
164
|
+
alert(input);
|
165
|
+
});
|
166
|
+
```
|
167
|
+
|
168
|
+
These can then be called with the `bindFunc` helper
|
169
|
+
|
170
|
+
```erb
|
171
|
+
<%=# some view %>
|
172
|
+
|
173
|
+
<%= bindFunc(:alertMe, @user.bind(:first_name));
|
174
|
+
```
|
175
|
+
|
176
|
+
|
142
177
|
## Including additional modules
|
143
178
|
|
144
179
|
Adding additional modules is just a matter of appending them to `sprinkles.requires`. This is already a feature of Angular and not Sprinkles
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/angular_sprinkles.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: angular_sprinkles 0.0.
|
5
|
+
# stub: angular_sprinkles 0.0.3 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "angular_sprinkles"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.3"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
@@ -1,9 +1,18 @@
|
|
1
1
|
//= require angular
|
2
2
|
|
3
|
+
(function (window, document, angular) {
|
4
|
+
|
5
|
+
var fnQueue = [];
|
6
|
+
|
3
7
|
window.sprinkles = angular.module('<%= AngularSprinkles::APP_NAME %>', []);
|
4
8
|
|
9
|
+
window.sprinkles.func = function () {
|
10
|
+
var args = [].slice.call(arguments, 0);
|
11
|
+
fnQueue.push({name: args[0], fn: args[1]});
|
12
|
+
};
|
13
|
+
|
5
14
|
window.onload = function () {
|
6
|
-
var
|
15
|
+
var queue,
|
7
16
|
app = angular.module('<%= AngularSprinkles::APP_NAME %>'),
|
8
17
|
doc = document.documentElement,
|
9
18
|
ctrlName = (doc.attributes['ng-controller'] || doc.attributes['data-ng-controller']) || 'Ctrl';
|
@@ -11,8 +20,16 @@ window.onload = function () {
|
|
11
20
|
ctrlName = ctrlName.split(' as ')[0];
|
12
21
|
|
13
22
|
<%= AngularSprinkles::CONTROLLER_FN %> = <%= AngularSprinkles::CONTROLLER_FN %> || function(){};
|
23
|
+
|
24
|
+
for (var i = 0; i < fnQueue.length; i++) {
|
25
|
+
queue = fnQueue[i];
|
26
|
+
<%= AngularSprinkles::CONTROLLER_FN %>.prototype[queue.name] = queue.fn;
|
27
|
+
}
|
28
|
+
|
14
29
|
app.controller(ctrlName, <%= AngularSprinkles::CONTROLLER_FN %>);
|
15
30
|
doc.setAttribute('data-ng-controller', ctrlName + ' as <%= AngularSprinkles::CONTROLLER_NAME %>');
|
16
31
|
|
17
32
|
angular.bootstrap(doc, [app.name]);
|
18
33
|
};
|
34
|
+
|
35
|
+
}(window, document, window.angular));
|
@@ -25,6 +25,11 @@ module AngularSprinkles
|
|
25
25
|
AngularSprinkles::Data::Bind.new(*input)
|
26
26
|
end
|
27
27
|
|
28
|
+
def bindFunc(fn, *args)
|
29
|
+
raise ArgumentError unless (fn.is_a?(String) || fn.is_a?(Symbol))
|
30
|
+
"#{AngularSprinkles::CONTROLLER_NAME}.#{fn}(#{args.join(',')})"
|
31
|
+
end
|
32
|
+
|
28
33
|
private
|
29
34
|
|
30
35
|
def build_chain(input)
|
@@ -7,95 +7,109 @@ describe AngularSprinkles::Helpers::BindHelper do
|
|
7
7
|
|
8
8
|
let(:stub) { StubClass.new }
|
9
9
|
|
10
|
-
|
10
|
+
describe '#bind' do
|
11
|
+
before { allow(stub).to receive(:content_for).and_return(true) }
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
context 'when 0 arguments' do
|
14
|
+
it 'raises an argument error' do
|
15
|
+
expect { stub.bind }.to raise_error(ArgumentError)
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
context 'when 1 argument' do
|
20
|
+
let(:var) { :brewhouse }
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
it 'returns the variable name given as a js string' do
|
23
|
+
expect(stub.bind(var).to_json).
|
24
|
+
to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{var}"))
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
it 'only yields the constructor definition to the view' do
|
28
|
+
expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
|
29
|
+
stub.bind(var)
|
30
|
+
end
|
29
31
|
end
|
30
|
-
end
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
context 'when 2 arguments' do
|
34
|
+
let(:vars) { [:brewhouse, :software] }
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
it 'returns the variables chained together as a js string' do
|
37
|
+
expect(stub.bind(*vars).to_json).
|
38
|
+
to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{vars.join('.')}"))
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
it 'yields the contructor definition and the first variable to the function to prototype' do
|
42
|
+
expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
|
43
|
+
expect(stub).to receive(:yield_to_sprinkles).
|
44
|
+
with("#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};")
|
44
45
|
|
45
|
-
|
46
|
+
stub.bind(*vars)
|
47
|
+
end
|
46
48
|
end
|
47
|
-
end
|
48
49
|
|
49
|
-
|
50
|
-
|
50
|
+
context 'when 3 arguments' do
|
51
|
+
let(:vars) { [:brewhouse, :software, :is_neat] }
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
it 'returns the variables chained together as a js string' do
|
54
|
+
expect(stub.bind(*vars).to_json).
|
55
|
+
to(eq("#{AngularSprinkles::CONTROLLER_NAME}.#{vars.join('.')}"))
|
56
|
+
end
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
it 'yields the constructor definition and the first variable to the function to prototype' do
|
59
|
+
expect(stub).to receive(:yield_to_sprinkles).with(AngularSprinkles::CONSTRUCTOR_DEFINITION)
|
60
|
+
expect(stub).to receive(:yield_to_sprinkles).
|
61
|
+
with(%{#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first} || {};
|
61
62
|
#{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first(2).join('.')} = #{AngularSprinkles::CONTROLLER_FN}.prototype.#{vars.first(2).join('.')} || {};})
|
62
63
|
|
63
|
-
|
64
|
+
stub.bind(*vars)
|
65
|
+
end
|
64
66
|
end
|
65
|
-
end
|
66
67
|
|
67
|
-
|
68
|
-
|
68
|
+
context 'when bind is called more than once' do
|
69
|
+
let(:vars) { [:brewhouse, :software, :is_neat] }
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
context 'and it is with the same variables' do
|
72
|
+
it 'yields the constructor definition and the first variable to the function to prototype' do
|
73
|
+
expect(stub).to receive(:content_for).twice
|
74
|
+
5.times { stub.bind(*vars) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'and it is called with different sets of variables' do
|
79
|
+
it 'yields the constructor definition, first variable, and a chain of the first two variables' do
|
80
|
+
expect(stub).to receive(:content_for).exactly(3).times
|
81
|
+
|
82
|
+
stub.bind(vars.first) # yields the constructor
|
83
|
+
stub.bind(vars.first) # yields nothing
|
84
|
+
stub.bind(*vars.first(2)) # yields declaration for :brewhouse
|
85
|
+
stub.bind(*vars) # yields declaration for [:brewhouse, :software]
|
86
|
+
stub.bind(*vars) # yields nothing
|
87
|
+
end
|
74
88
|
end
|
75
89
|
end
|
76
90
|
|
77
|
-
context '
|
78
|
-
|
79
|
-
|
91
|
+
context 'when the constructor has already been yielded' do
|
92
|
+
let(:var) { :brewhouse }
|
93
|
+
|
94
|
+
before { allow(stub).to receive(:app_initialized?).and_return(true) }
|
80
95
|
|
81
|
-
|
82
|
-
stub.
|
83
|
-
stub.bind(
|
84
|
-
stub.bind(*vars) # yields declaration for [:brewhouse, :software]
|
85
|
-
stub.bind(*vars) # yields nothing
|
96
|
+
it 'does not yield anything with only one argument' do
|
97
|
+
expect(stub).not_to receive(:content_for)
|
98
|
+
stub.bind(var)
|
86
99
|
end
|
87
100
|
end
|
88
101
|
end
|
89
102
|
|
90
|
-
|
91
|
-
let(:
|
103
|
+
describe '#bindFunc' do
|
104
|
+
let(:fn) { :brewhouse }
|
105
|
+
let(:input) { [5, 'software'] }
|
92
106
|
|
93
|
-
|
107
|
+
it 'returns a javascript function binding string' do
|
108
|
+
expect(stub.bindFunc(fn, *input)).to eq("#{AngularSprinkles::CONTROLLER_NAME}.#{fn}(#{input.join(',')})")
|
109
|
+
end
|
94
110
|
|
95
|
-
it '
|
96
|
-
expect
|
97
|
-
stub.bind(var)
|
111
|
+
it 'raises with an input' do
|
112
|
+
expect { stub.bindFunc }.to raise_error(ArgumentError)
|
98
113
|
end
|
99
114
|
end
|
100
|
-
|
101
115
|
end
|