jelly 0.5.7 → 0.5.8

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.markdown CHANGED
@@ -12,6 +12,14 @@ Jelly encourages and enables unit testing your Javascript code. Using a Javascri
12
12
  or [Screw Unit](http://github.com/nathansobo/screw-unit), Jelly allows you to test AJAX and client-side
13
13
  events independently from your Rails app.
14
14
 
15
+ Key Benefits
16
+ ------------
17
+ * [Unobtrusive Javascript](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript). Your Javascript code remains completely
18
+ separate from your markup.
19
+ * [Test Driven Development](http://en.wikipedia.org/wiki/Test-driven_development). Jelly blends well with the Javascript testing framework
20
+ [Jasmine](http://github.com/pivotal/jasmine) and allows you to test-drive your ajaxy and client-side code.
21
+ * Familiar conventions. Jelly follows the conventions of Ruby on Rails, making it simple for developers to organize and keep track of their Javascript code.
22
+
15
23
  What Jelly is NOT
16
24
  -----------------
17
25
  **Jelly is NOT a Javascript generator.** With Jelly, you're writing pure Javascript to define your AJAX browser events. Jelly simply
@@ -41,7 +49,7 @@ Getting Started
41
49
 
42
50
  Be sure to require <code>jelly</code> when your application loads. This can be done in your `environment.rb` in the `Rails::Initializer.run` block:
43
51
 
44
- config.gem "jelly"
52
+ config.gem 'jelly'
45
53
 
46
54
  Then, in your layouts, add the following to the `<head>` section:
47
55
 
@@ -54,16 +62,16 @@ expansion instead
54
62
 
55
63
  The `spread_jelly` line activates the events that you have defined on the current page.
56
64
 
57
- Usage
65
+ Basic Usage
58
66
  -------------
59
67
 
60
- Jelly maps page-specific Javascript functions to Rails Actions and Controllers. For example: FunController#index will
68
+ Jelly maps page-specific Javascript functions to Rails Actions and Controllers. For example: StoriesController#index will
61
69
  activate the `index` function in the `Fun` Jelly object. Jelly uses jQuery's `$(document).ready()` to execute the
62
70
  page-specifc function when the page has loaded. Let's look at some code:
63
71
 
64
- In `public/javascripts/pages/fun.js`, I write a simple Jelly file:
72
+ In public/javascripts/pages/stories.js, we create a simple Jelly file:
65
73
 
66
- Jelly.add("Fun", {
74
+ Jelly.add("Stories", {
67
75
 
68
76
  index: function() {
69
77
  $('a.clickme').click(function() {
@@ -73,11 +81,11 @@ In `public/javascripts/pages/fun.js`, I write a simple Jelly file:
73
81
 
74
82
  });
75
83
 
76
- Jelly will automatically execute the `index` function when the Rails app runs the `FunController#index` action. Lets
84
+ Jelly will automatically execute the `index` function when the Rails app runs the `StoriesController#index` action. Lets
77
85
  continue the example by adding more Javascript functions that map to the `new` and `show` Rails actions. We can also
78
- specify an `all` function, which will be executed on all actions in the `FunController`.
86
+ specify an `all` function, which will be executed on all actions in the `StoriesController`.
79
87
 
80
- Jelly.add("Fun", {
88
+ Jelly.add("Stories", {
81
89
 
82
90
  index: function() {
83
91
  $('a.clickme').click(function() {
@@ -100,62 +108,239 @@ specify an `all` function, which will be executed on all actions in the `FunCont
100
108
  Notice the slightly different syntax for `new`. This is because `new` is a reserved word in Javascript.
101
109
  Create a separate file in `public/javascripts/pages` for each of your controllers as you use Jelly throughout your application.
102
110
 
111
+ Common Components
112
+ -----------------
113
+
114
+ Often you will want to mix common Javascript components on many pages throughout your application, not just in the namespace
115
+ of a single Controller. Jelly Components allow you to organize common Javascript code, and invoke it on arbitrary pages
116
+ within your application.
117
+
118
+ Jelly Components are simply Javascript classes with (at least) an `init` function. Here's an example of a `SearchBox` component that
119
+ activates an autocompleter on a search box on every page.
120
+
121
+ in public/javascripts/components/search_box.js:
122
+
123
+ SearchBox = {
124
+ init: function(){
125
+ $("#search_box").autocompleter({
126
+ url : '/search'
127
+ });
128
+ }
129
+ };
130
+
131
+ To attach the SearchBox component to the page and automatically call the `init` function when the page is ready, we use the `attach_javascript_component`
132
+ method in our view. This can be done either in your layout (for components to attach to all pages), or in your view using
133
+ `content_for`.
134
+
135
+ in the `<head>` tag of the layout:
136
+
137
+ <%= attach_javascript_component('SearchBox') %>
138
+
139
+ or in a view:
140
+
141
+ <% content_for :javascript do -%>
142
+ <%= attach_javascript_component('SearchBox') %>
143
+ <% end -%>
144
+
145
+ Components always get initialized **after** the page-specific Javascript functions.
146
+
103
147
  AJAX With Jelly
104
148
  ---------------
105
149
 
106
- You can trigger callbacks on the page object from Rails with the `jelly_callback` method.
107
- Adding to the `index.html.erb` file from above:
150
+ Jelly adds an `$.ajaxWithJelly()` function to the jQuery namespace which is a simple wrapper for jQuery's `$.ajax()`.
151
+ When you use `$.ajaxWithJelly()` to create an ajax event, Jelly automatically adds an onSuccess handler to your ajax
152
+ call that invokes the Jelly framework after receiving the ajax response.
153
+
154
+ Jelly's convention relies on the Controller to specify the javascript callback after an ajax request. We can invoke Jelly
155
+ in response to a javascript request with the `jelly_callback` method.
156
+
157
+ This example assumes that you have working knowledge of jQuery's `$.ajax()` function. If not, [read up on it here](http://docs.jquery.com/Ajax).
108
158
 
109
- <a href="#" id="jelly_ajax_link">Click me for Jelly Ajax Action</a>
110
- <span id="jelly_callback_element">This gets filled in by the Jelly Ajax callback</span>
159
+ ### Simple AJAX example with `$.ajaxWithJelly()` and `jelly_callback`
111
160
 
112
- And update your controller:
161
+ The view, new.html.erb:
113
162
 
114
- class FunController < ApplicationController
115
- def index
163
+ <a href="#" id="create_story_link">create story</a>
164
+
165
+ The controller, stories_controller.rb
166
+
167
+ class StoriesController < ApplicationController
168
+ def new
116
169
  end
117
170
 
118
- def ajax_action
119
- jelly_callback do
120
- [
121
- render(:partial => 'fun_partial'),
122
- "second_parameter"
123
- ]
171
+ def create
172
+ Story.create!(params[:story])
173
+ respond_to do |format|
174
+ format.html
175
+ format.js { jelly_callback }
124
176
  end
125
177
  end
126
178
  end
127
179
 
128
- Update your page object in `fun.js`:
180
+ The javascript, pages/stories.js:
129
181
 
130
- Jelly.add("Fun", {
131
- all: function() {
132
- $('title').text("Hello! Isn't this fun?");
182
+ Jelly.add("Stories", {
183
+
184
+ new: function() {
185
+ $("#create_story_link").click(function() {
186
+ $.ajaxWithJelly({
187
+ url: "/stories",
188
+ data: {
189
+ name : 'Untitled Story',
190
+ }
191
+ });
192
+ });
133
193
  },
134
- index: function() {
135
- $('h1').text("Welcome to the index page.");
136
- $("#jelly_ajax_link").click(function() {
194
+
195
+ on_create: function() {
196
+ alert('Your story has been created!');
197
+ }
198
+ });
199
+
200
+ The example above attaches an ajax event to the "create story" link, and when clicked, jQuery will fire a ajax POST request to
201
+ the create action of our controller. The controller then responds with `jelly_callback`, and by default invokes the
202
+ javascript function named `on_create` in the Stories javascript file.
203
+
204
+ ### Passing parameters to the Jelly callback target
205
+
206
+ If we wanted to make the creation of the story a bit more interesting, we can send back a html fragment of the
207
+ new story that has been created, and pass it as a parameter to `on_create` so it can be added to the page. Let's see how that might look:
208
+
209
+ The view, new.html.erb:
210
+
211
+ <a href="#" id="create_story_link">create story</a>
212
+ <ul id="stories">
213
+ <li>First Story</li>
214
+ </ul>
215
+
216
+ The controller, stories_controller.rb
217
+
218
+ class StoriesController < ApplicationController
219
+ def new
220
+ end
221
+
222
+ def create
223
+ Story.create!(params[:story])
224
+ respond_to do |format|
225
+ format.html
226
+ format.js do
227
+ jelly_callback do
228
+ render :partial => 'story_list_item'
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end
234
+
235
+ The javascript, pages/stories.js:
236
+
237
+ Jelly.add("Stories", {
238
+
239
+ new: function() {
240
+ $("#create_story_link").click(function() {
137
241
  $.ajaxWithJelly({
138
- type: "GET",
139
- url: "/fun/ajax_action"
242
+ url: "/stories",
243
+ data: {
244
+ name : 'Untitled Story',
245
+ }
140
246
  });
141
247
  });
142
248
  },
143
- on_ajax_action: function(html, second_parameter) {
144
- $('#jelly_callback_element').html(html);
249
+
250
+ on_create: function(storyListItemHtml) {
251
+ $("#stories").append(storyListItemHtml);
145
252
  }
146
253
  });
147
254
 
148
- And finally, make the partial `_fun_partial.html.erb` and just put "Hello from the server!" in it, then visit your page
149
- and watch the ajax callbacks in action.
255
+ The `jelly_callback` function accepts a block which is evaluated in the context of the view layer, which allows you to
256
+ render partials and use Rails Helpers as you normally would. You can pass as many parameters as you want to the javascript
257
+ callback by passing an array to the `jelly_callback` block:
258
+
259
+ ### Passing multiple parameters to the Jelly callback target
260
+
261
+ in the controller, stories_controller.rb:
262
+
263
+ def create
264
+ @story = Story.create!(params[:story])
265
+ respond_to do |format|
266
+ format.html
267
+ format.js do
268
+ jelly_callback do
269
+ [ render(:partial => 'story_list_item'), @story.id, "Nice looking story, smart guy" ]
270
+ end
271
+ end
272
+ end
273
+ end
274
+
275
+ in the javascript, pages/stories.js:
276
+
277
+ on_create: function(storyListItemHtml, storyId, message) {
278
+ $(storyListItemHtml).attr('id', storyId).appendTo($("#stories"));
279
+ alert(message);
280
+ },
281
+
282
+ ### Specifying custom callback functions in jelly_callback
150
283
 
151
- The `jelly_callback` method takes an optional parameter for the name of the callback, and the provided block can return
152
- either one parameter, or an array of parameters.
284
+ As we have seen above, by default, `jelly_callback` invokes the javascript function by prepending `on_` to the Rails
285
+ action name. The `jelly_callback` method can take an optional parameter for the name of the callback to allow more fine-grained
286
+ client-side behaviors depending on the server-side response.
153
287
 
154
- DEVELOPMENT
155
- -----------
288
+ in the controller, stories_controller.rb
289
+
290
+ def create
291
+ begin
292
+ Story.create!(params[:story])
293
+ respond_to do |format|
294
+ format.html
295
+ format.js do
296
+ jelly_callback('successful_create') do
297
+ render :partial => 'story_list_item'
298
+ end
299
+ end
300
+ end
301
+ rescue
302
+ respond_to do |format|
303
+ format.html
304
+ format.js do
305
+ jelly_callback('failed_create')
306
+ end
307
+ end
308
+ end
309
+ end
310
+
311
+ in the javascript, pages/stories.js:
312
+
313
+ on_successful_create: function(storyListItemHtml) {
314
+ $("#stories").append(storyListItemHtml);
315
+ },
316
+
317
+ on_failed_create: function() {
318
+ alert('Oops, there was a problem creating your story!);
319
+ }
320
+
321
+ ### Callbacks to Jelly Components
322
+
323
+ By default, ajax callbacks functions are scoped to the current Jelly page. But if you want, you can also direct ajax
324
+ callbacks to functions on Jelly components or other Javascript objects in your application. To
325
+ do this, send an `:on` paremeter to `jelly_callback`, for example.
326
+
327
+ in the controller:
328
+
329
+ respond_to do |format|
330
+ format.js do
331
+ jelly_callback('successful_create', :on => 'CommonHandler') do
332
+ render :partial => 'story_list_item'
333
+ end
334
+ end
335
+ end
336
+
337
+ This will call `CommonHandler.on_successful_create()` with the response.
338
+
339
+ Jelly Development
340
+ -----------------
156
341
 
157
342
  Track Jelly's development roadmap on [Jelly's Pivotal Tracker project](http://www.pivotaltracker.com/projects/30454)
158
343
 
159
344
  To run ruby tests, run `rake spec`.
160
345
 
161
- To run Javascript tests, open `jelly/spec/jasmine_runner.html` in a web browser.
346
+ To run Javascript tests, open `jelly/spec/jasmine_runner.html` in Firefox or Safari.
data/VERSION.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
- :patch: 7
3
- :major: 0
4
2
  :minor: 5
3
+ :patch: 8
4
+ :build:
5
+ :major: 0
@@ -48,14 +48,14 @@ Jelly.initComponents = function() {
48
48
  Jelly.notifyObservers = function(params) {
49
49
  var context = params.on ? eval(params.on) : page;
50
50
  if(context[params.method]) {
51
- context[params.method].apply(params, params.arguments);
51
+ context[params.method].apply(context, params.arguments);
52
52
  }
53
53
  $.protify(page.components).each(function(componentAndArgs) {
54
54
  var component = componentAndArgs[0];
55
55
  if(component[params.method]) {
56
56
  component[params.method].apply(component, params.arguments);
57
57
  }
58
- });
58
+ });
59
59
  };
60
60
 
61
61
  Jelly.Page = function(name) {
data/jelly.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jelly}
8
- s.version = "0.5.7"
8
+ s.version = "0.5.8"
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-10-08}
12
+ s.date = %q{2009-10-28}
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 = [
@@ -43,23 +43,23 @@ Gem::Specification.new do |s|
43
43
  s.summary = %q{a sweet unobtrusive javascript framework for jQuery and Rails}
44
44
  s.test_files = [
45
45
  "spec/controllers/jelly_controller_spec.rb",
46
- "spec/spec_helper.rb",
46
+ "spec/helpers/jelly_helper_spec.rb",
47
47
  "spec/rails_root/app/controllers/application_controller.rb",
48
48
  "spec/rails_root/app/helpers/application_helper.rb",
49
+ "spec/rails_root/config/boot.rb",
49
50
  "spec/rails_root/config/environment.rb",
50
- "spec/rails_root/config/initializers/new_rails_defaults.rb",
51
- "spec/rails_root/config/initializers/mime_types.rb",
52
- "spec/rails_root/config/initializers/session_store.rb",
53
- "spec/rails_root/config/initializers/backtrace_silencers.rb",
54
- "spec/rails_root/config/initializers/inflections.rb",
51
+ "spec/rails_root/config/environments/development.rb",
55
52
  "spec/rails_root/config/environments/production.rb",
56
53
  "spec/rails_root/config/environments/test.rb",
57
- "spec/rails_root/config/environments/development.rb",
54
+ "spec/rails_root/config/initializers/backtrace_silencers.rb",
55
+ "spec/rails_root/config/initializers/inflections.rb",
56
+ "spec/rails_root/config/initializers/mime_types.rb",
57
+ "spec/rails_root/config/initializers/new_rails_defaults.rb",
58
+ "spec/rails_root/config/initializers/session_store.rb",
58
59
  "spec/rails_root/config/routes.rb",
59
- "spec/rails_root/config/boot.rb",
60
60
  "spec/rails_root/test/performance/browsing_test.rb",
61
61
  "spec/rails_root/test/test_helper.rb",
62
- "spec/helpers/jelly_helper_spec.rb"
62
+ "spec/spec_helper.rb"
63
63
  ]
64
64
 
65
65
  if s.respond_to? :specification_version then
@@ -75,3 +75,4 @@ Gem::Specification.new do |s|
75
75
  s.add_dependency(%q<rails>, [">= 2.3.0"])
76
76
  end
77
77
  end
78
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jelly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.5.8
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-10-08 00:00:00 -07:00
12
+ date: 2009-10-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -80,20 +80,20 @@ specification_version: 3
80
80
  summary: a sweet unobtrusive javascript framework for jQuery and Rails
81
81
  test_files:
82
82
  - spec/controllers/jelly_controller_spec.rb
83
- - spec/spec_helper.rb
83
+ - spec/helpers/jelly_helper_spec.rb
84
84
  - spec/rails_root/app/controllers/application_controller.rb
85
85
  - spec/rails_root/app/helpers/application_helper.rb
86
+ - spec/rails_root/config/boot.rb
86
87
  - spec/rails_root/config/environment.rb
87
- - spec/rails_root/config/initializers/new_rails_defaults.rb
88
- - spec/rails_root/config/initializers/mime_types.rb
89
- - spec/rails_root/config/initializers/session_store.rb
90
- - spec/rails_root/config/initializers/backtrace_silencers.rb
91
- - spec/rails_root/config/initializers/inflections.rb
88
+ - spec/rails_root/config/environments/development.rb
92
89
  - spec/rails_root/config/environments/production.rb
93
90
  - spec/rails_root/config/environments/test.rb
94
- - spec/rails_root/config/environments/development.rb
91
+ - spec/rails_root/config/initializers/backtrace_silencers.rb
92
+ - spec/rails_root/config/initializers/inflections.rb
93
+ - spec/rails_root/config/initializers/mime_types.rb
94
+ - spec/rails_root/config/initializers/new_rails_defaults.rb
95
+ - spec/rails_root/config/initializers/session_store.rb
95
96
  - spec/rails_root/config/routes.rb
96
- - spec/rails_root/config/boot.rb
97
97
  - spec/rails_root/test/performance/browsing_test.rb
98
98
  - spec/rails_root/test/test_helper.rb
99
- - spec/helpers/jelly_helper_spec.rb
99
+ - spec/spec_helper.rb