honkster-jelly 0.8.4 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 8
3
- :patch: 4
3
+ :patch: 6
4
4
  :major: 0
@@ -2,7 +2,7 @@
2
2
  * Jelly. a sweet unobtrusive javascript framework
3
3
  * for jQuery and Rails
4
4
  *
5
- * version 0.8.4
5
+ * version 0.8.6
6
6
  *
7
7
  * Copyright (c) 2009 Pivotal Labs
8
8
  * Licensed under the MIT license.
@@ -20,150 +20,132 @@ if (!Function.prototype.bind) {
20
20
  }
21
21
  }
22
22
  }
23
- Jelly.init = function() {
24
- this.observers = [];
25
- this.observers.pending = [];
26
- this.attach = this.Observers.attach;
27
- this.notifyObservers = this.Observers.notify;
28
- this.Components.initCalled = false;
29
- this.Pages.init();
30
- var self = this;
31
- $(document).ready(function() {
32
- self.Components.init();
33
- });
34
- };
35
-
36
- Jelly.Components = {
23
+ $.extend(Jelly, {
37
24
  init: function() {
38
- for (var i = 0; i < Jelly.observers.pending.length; i++) {
39
- Jelly.Observers.initPending.call(Jelly.observers, Jelly.observers.pending[i]);
40
- }
41
- this.initCalled = true;
42
- }
43
- };
25
+ this.observers = [];
26
+ this.attach = this.Observers.attach;
27
+ this.notifyObservers = this.Observers.notify;
28
+ this.Pages.init();
29
+ },
44
30
 
45
- Jelly.Observers = {
46
- attach: function() {
47
- if (this == Jelly) {
48
- return Jelly.Observers.attach.apply(this.observers, arguments);
49
- }
50
- this.pending = this.pending || [];
51
- for (var i = 0; i < arguments.length; i++) {
52
- var definition = arguments[i];
53
- var component = (typeof definition.component == "string") ?
54
- eval(definition.component) :
55
- definition.component;
56
- var evaluatedDefinition = {
57
- component: component,
58
- arguments: definition.arguments
59
- };
60
- this.pending.push(evaluatedDefinition);
61
- if (Jelly.Components.initCalled) {
62
- Jelly.Observers.initPending.call(this, evaluatedDefinition);
31
+ Observers: {
32
+ attach: function() {
33
+ if (this == Jelly) {
34
+ return Jelly.Observers.attach.apply(this.observers, arguments);
63
35
  }
64
- }
65
- },
36
+ for (var i = 0; i < arguments.length; i++) {
37
+ var definitionOrComponent = arguments[i];
38
+ var observer;
39
+ if (definitionOrComponent.component) {
40
+ var component = Jelly.Observers.evaluateComponent(definitionOrComponent.component);
41
+ if (component.init) {
42
+ observer = component.init.apply(component, definitionOrComponent.arguments) || component;
43
+ } else {
44
+ observer = component;
45
+ }
46
+ } else {
47
+ observer = Jelly.Observers.evaluateComponent(definitionOrComponent);
48
+ }
49
+ this.push(observer);
50
+ }
51
+ },
66
52
 
67
- initPending: function(definition) {
68
- var observer;
69
- if (definition.component.init) {
70
- observer = definition.component.init.apply(definition.component, definition.arguments);
71
- }
72
- this.push(observer ? observer : definition.component);
73
- },
53
+ evaluateComponent: function(component) {
54
+ return (typeof component == "string") ? eval(component) : component;
55
+ },
74
56
 
75
- notify: function(callbacks) {
76
- if (this == Jelly) {
77
- return Jelly.Observers.notify.apply(this.observers, arguments);
78
- }
79
- if (!$.isArray(callbacks)) {
80
- callbacks = [callbacks];
81
- }
57
+ notify: function(callbacks) {
58
+ if (this == Jelly) {
59
+ return Jelly.Observers.notify.apply(this.observers, arguments);
60
+ }
61
+ if (!$.isArray(callbacks)) {
62
+ callbacks = [callbacks];
63
+ }
82
64
 
83
- var observers = this.slice(0);
84
- for (var i = 0; i < callbacks.length; i++) {
85
- var callback = callbacks[i];
65
+ var observers = this.slice(0);
66
+ for (var i = 0; i < callbacks.length; i++) {
67
+ var callback = callbacks[i];
86
68
 
87
- // Deprecate 'on' in favor of making each page action a Component.
88
- if (callback.on) {
89
- var additionalObserver = eval(callback.on);
90
- if (observers.indexOf(additionalObserver) == -1) {
91
- observers.push(additionalObserver);
69
+ // Deprecate 'on' in favor of making each page action a Component.
70
+ if (callback.on) {
71
+ var additionalObserver = eval(callback.on);
72
+ if (observers.indexOf(additionalObserver) == -1) {
73
+ observers.push(additionalObserver);
74
+ }
92
75
  }
93
- }
94
76
 
95
- for (var j = 0; j < observers.length; j++) {
96
- var observer = observers[j];
97
- if (observer[callback.method]) {
98
- if (observer.detach && observer.detach()) {
99
- Jelly.Observers.garbageCollectObserver.call(this, observer);
100
- } else {
101
- observer[callback.method].apply(observer, callback.arguments);
77
+ if (callback.method) {
78
+ for (var j = 0; j < observers.length; j++) {
79
+ var observer = observers[j];
80
+ if (observer[callback.method]) {
81
+ if (observer.detach && observer.detach()) {
82
+ Jelly.Observers.garbageCollectObserver.call(this, observer);
83
+ } else {
84
+ observer[callback.method].apply(observer, callback.arguments);
85
+ }
86
+ }
102
87
  }
103
88
  }
89
+
90
+ if (callback.attach) {
91
+ Jelly.Observers.attach.apply(this, callback.attach);
92
+ }
104
93
  }
94
+ },
105
95
 
106
- if (callback.attach) {
107
- Jelly.Observers.attach.apply(this, callback.attach);
96
+ garbageCollectObserver: function(observer) {
97
+ var index = this.indexOf(observer);
98
+ if (index > -1) {
99
+ Jelly.Observers.remove.call(this, index, index + 1);
108
100
  }
109
- }
110
- },
101
+ },
111
102
 
112
- garbageCollectObserver: function(observer) {
113
- var index = this.indexOf(observer);
114
- if (index > -1) {
115
- Jelly.Observers.remove.call(this, index, index + 1);
103
+ remove: function(from, to) {
104
+ var rest = this.slice((to || from) + 1 || this.length);
105
+ this.length = from < 0 ? this.length + from : from;
106
+ return this.push.apply(this, rest);
116
107
  }
117
108
  },
118
109
 
119
- remove: function(from, to) {
120
- var rest = this.slice((to || from) + 1 || this.length);
121
- this.length = from < 0 ? this.length + from : from;
122
- return this.push.apply(this, rest);
123
- }
124
- };
110
+ Pages: {
111
+ init: function() {
112
+ this.all = {};
113
+ Jelly.all = this.all; // Deprecated
114
+ },
125
115
 
126
- Jelly.Pages = {
127
- init: function() {
128
- this.all = {};
129
- Jelly.all = this.all; // Deprecated
130
- },
131
-
132
- add: function(name) {
133
- var page = new Jelly.Page.Constructor(name);
134
- for (var i = 1; i < arguments.length; i++) {
135
- $.extend(page, arguments[i]);
116
+ add: function(name) {
117
+ var page = new Jelly.Page.Constructor(name);
118
+ for (var i = 1; i < arguments.length; i++) {
119
+ $.extend(page, arguments[i]);
120
+ }
121
+ return page;
136
122
  }
137
- return page;
138
- }
139
- };
140
- Jelly.add = Jelly.Pages.add; // Deprecated
141
-
142
- Jelly.Page = {
143
- init: function(controllerName, actionName) {
144
- var page = Jelly.Pages.all[controllerName] || new Jelly.Page.Constructor(controllerName);
145
- window.page = page;
146
- if (page.all) page.all();
147
- if (page[actionName]) page[actionName].call(page);
148
- page.loaded = true;
149
- return page;
150
123
  },
151
- Constructor: function(name) {
152
- this.loaded = false;
153
- this.documentHref = Jelly.Location.documentHref;
154
-
155
- this.name = name;
156
- Jelly.Pages.all[name] = this;
157
- }
158
- };
159
124
 
160
- Jelly.Location = {
161
- init: function() {
125
+ Page: {
126
+ init: function(controllerName, actionName) {
127
+ var page = Jelly.Pages.all[controllerName] || new Jelly.Page.Constructor(controllerName);
128
+ window.page = page;
129
+ if (page.all) page.all();
130
+ if (page[actionName]) page[actionName].call(page);
131
+ page.loaded = true;
132
+ return page;
133
+ },
134
+ Constructor: function(name) {
135
+ this.loaded = false;
136
+ this.documentHref = Jelly.Location.documentHref;
137
+
138
+ this.name = name;
139
+ Jelly.Pages.all[name] = this;
140
+ }
162
141
  },
163
142
 
164
- on_redirect: function(location) {
165
- top.location.href = location;
143
+ Location: {
144
+ on_redirect: function(location) {
145
+ top.location.href = location;
146
+ }
166
147
  }
167
- };
148
+ });
149
+ Jelly.add = Jelly.Pages.add; // Deprecated
168
150
 
169
151
  Jelly.init();
data/jelly.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jelly}
8
- s.version = "0.8.4"
8
+ s.version = "0.8.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Pivotal Labs, Inc"]
12
- s.date = %q{2009-12-29}
12
+ s.date = %q{2010-01-05}
13
13
  s.description = %q{Jelly provides a set of tools and conventions for creating rich ajax/javascript web applications with jQuery and Ruby on Rails.}
14
14
  s.email = %q{opensource@pivotallabs.com}
15
15
  s.extra_rdoc_files = [
@@ -12,10 +12,10 @@ module JellyController
12
12
 
13
13
  def raw_jelly_callback(options={}, &block)
14
14
  options.symbolize_keys!
15
- options[:format] ||= if request.xhr?
16
- :json
17
- elsif params[:callback]
15
+ options[:format] ||= if params[:callback]
18
16
  :jsonp
17
+ elsif request.xhr?
18
+ :json
19
19
  else
20
20
  :iframe
21
21
  end
@@ -17,7 +17,6 @@ module JellyHelper
17
17
  <<-HTML
18
18
  #{window_token_javascript_tag}
19
19
  #{attach_javascript_component_javascript_tag(jelly_attached_components)}
20
- #{attach_javascript_component_on_document_ready_javascript_tag(jelly_attached_components_on_ready)}
21
20
  HTML
22
21
  end
23
22
 
@@ -26,11 +25,6 @@ module JellyHelper
26
25
  end
27
26
 
28
27
  def attach_javascript_component_javascript_tag(*components)
29
- components = [components].flatten
30
- javascript_tag("Jelly.attach.apply(Jelly, #{components.to_json});")
31
- end
32
-
33
- def attach_javascript_component_on_document_ready_javascript_tag(*components)
34
28
  components = [components].flatten
35
29
  javascript_tag <<-JS
36
30
  $(document).ready(function() {
@@ -51,18 +45,12 @@ module JellyHelper
51
45
  end
52
46
 
53
47
  def attach_javascript_component_on_ready(component_name, *args)
54
- key = jelly_attach_component_definition_hash(component_name, *args)
55
- unless jelly_attached_components_on_ready.include? key
56
- jelly_attached_components_on_ready << key
57
- end
48
+ warn "attach_javascript_component_on_ready is deprecated since attach_javascript_component adds components to be attached in a $(document).ready block\n#{puts caller.join("\n\t")}"
49
+ attach_javascript_component(component_name, *args)
58
50
  end
59
51
 
60
52
  def jelly_attached_components
61
53
  @jelly_attached_components ||= []
62
54
  end
63
55
 
64
- def jelly_attached_components_on_ready
65
- @jelly_attached_components_on_ready ||= []
66
- end
67
-
68
56
  end
@@ -123,31 +123,31 @@ describe ApplicationController do
123
123
  end
124
124
  end
125
125
 
126
- context "when the request is XHR" do
126
+ context "when there is a callback param" do
127
127
  before do
128
- stub(request).xhr? {true}
128
+ @controller.params[:callback] = "Jelly.notifyObservers"
129
129
  end
130
130
 
131
- it "responds with a json hash" do
132
- @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
133
- "grape"
131
+ context "when the request is XHR" do
132
+ before do
133
+ stub(request).xhr? {true}
134
134
  end
135
- callback = JSON.parse(response.body)
136
- callback["method"].should == "on_foo"
137
- callback["arguments"].should == ["grape"]
138
- callback["bar"].should == "baz"
139
- end
140
-
141
- end
142
135
 
143
- context "when the request is not XHR" do
144
- before do
145
- stub(request).xhr? {false}
136
+ it "responds with a call to the given callback method with the json as an argument" do
137
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
138
+ "grape"
139
+ end
140
+ json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1]
141
+ callback = JSON.parse(json)
142
+ callback["method"].should == "on_foo"
143
+ callback["arguments"].should == ["grape"]
144
+ callback["bar"].should == "baz"
145
+ end
146
146
  end
147
147
 
148
- context "when there is a callback param" do
148
+ context "when the request is not XHR" do
149
149
  before do
150
- @controller.params[:callback] = "Jelly.notifyObservers"
150
+ stub(request).xhr? {false}
151
151
  end
152
152
 
153
153
  it "responds with a call to the given callback method with the json as an argument" do
@@ -161,22 +161,47 @@ describe ApplicationController do
161
161
  callback["bar"].should == "baz"
162
162
  end
163
163
  end
164
+ end
164
165
 
165
- context "when there is not a callback param" do
166
- it "wraps the json response in a textarea tag to support File Uploads in an iframe target (see: http://malsup.com/jquery/form/#code-samples)" do
166
+ context "when there is not a callback param" do
167
+ context "when the request is XHR" do
168
+ before do
169
+ stub(request).xhr? {true}
170
+ end
171
+
172
+ it "responds with a json hash" do
167
173
  @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
168
174
  "grape"
169
175
  end
170
- body = response.body
171
- body.should =~ /^<textarea>/
172
- body.should =~ /<\/textarea>$/
173
- doc = Nokogiri::HTML(body)
174
-
175
- callback = JSON.parse(doc.at("textarea").inner_html)
176
+ callback = JSON.parse(response.body)
176
177
  callback["method"].should == "on_foo"
177
178
  callback["arguments"].should == ["grape"]
178
179
  callback["bar"].should == "baz"
179
180
  end
181
+
182
+ end
183
+
184
+ context "when the request is not XHR" do
185
+ before do
186
+ stub(request).xhr? {false}
187
+ end
188
+
189
+ context "when there is not a callback param" do
190
+ it "wraps the json response in a textarea tag to support File Uploads in an iframe target (see: http://malsup.com/jquery/form/#code-samples)" do
191
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
192
+ "grape"
193
+ end
194
+ body = response.body
195
+ body.should =~ /^<textarea>/
196
+ body.should =~ /<\/textarea>$/
197
+ doc = Nokogiri::HTML(body)
198
+
199
+ callback = JSON.parse(doc.at("textarea").inner_html)
200
+ callback["method"].should == "on_foo"
201
+ callback["arguments"].should == ["grape"]
202
+ callback["bar"].should == "baz"
203
+ end
204
+ end
180
205
  end
181
206
  end
182
207
  end
@@ -67,7 +67,7 @@ describe JellyHelper do
67
67
  ]
68
68
  end
69
69
 
70
- it "adds a call to Jelly.attach in the javascript content" do
70
+ it "adds a call to Jelly.attach in an $(document).ready block" do
71
71
  helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
72
72
  expected_args = ['arg1','arg2','arg3'].to_json
73
73
  assigns[:jelly_attached_components].should == [
@@ -76,26 +76,8 @@ describe JellyHelper do
76
76
 
77
77
  html = helper.spread_jelly
78
78
  doc = Nokogiri::HTML(html)
79
- pre_document_ready_tag = doc.css("script")[1]
80
- pre_document_ready_tag.inner_html.should_not include("$(document).ready(function() {")
81
- pre_document_ready_part = pre_document_ready_tag.inner_html.split("\n")[2]
82
-
83
- arguments = jelly_attach_arguments(pre_document_ready_part)
84
- arguments.should include({'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']})
85
- end
86
-
87
- it "adds a call to Jelly.attach in the javascript_on_ready content" do
88
- helper.attach_javascript_component_on_ready("MyComponent", 'arg1', 'arg2', 'arg3')
89
-
90
- assigns[:jelly_attached_components_on_ready].should == [
91
- {'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']}
92
- ]
93
-
94
- html = helper.spread_jelly
95
- doc = Nokogiri::HTML(html)
96
- document_ready_tag = doc.css("script")[2]
79
+ document_ready_tag = doc.css("script")[1]
97
80
  document_ready_tag.inner_html.should include("$(document).ready(function() {")
98
-
99
81
  document_ready_part = document_ready_tag.inner_html.split("\n")[3]
100
82
  arguments = jelly_attach_arguments(document_ready_part)
101
83
  arguments.should include({'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honkster-jelly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pivotal Labs, Inc
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-29 00:00:00 -08:00
12
+ date: 2010-01-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency