Marcel Molina Jr. Presentation
-
Upload
rubyonrailsdude -
Category
Business
-
view
1.723 -
download
0
description
Transcript of Marcel Molina Jr. Presentation
Sharing RJS: Reuse at the app level
Marcel Molina Jr.37signals
1
A little bit about me
2
Member of Rails Core
Sam StephensonRick Olson
Thomas FuchsJamis Buck
Tobias Lütke Nicholas Seckar
Jeremy Kemper
Some of the people you trust your business to...
Michael Koziarski
3
Work for 37signals
4
Hottest Hacker On Earth
David Has And Belongs To Many Hanssons
5
Sharing RJS
6
Typical RJS
app/controllers/articles_controller.rb
7
app/controllers/articles_controller.rb
Duplication
Oops
8
Let’s fix this
9
Extract to private method of controller
app/controllers/articles_controller.rb
10
app/controllers/articles_controller.rb
Wrong scope!
Oops
11
Extract to helper, passing page
app/helpers/articles_helper.rb
12
This works
app/controllers/articles_controller.rb
13
But it’s Pythonic :-(
Oops
app/controllers/articles_controller.rb
14
Monkey patch JavaScriptGenerator!!!
15
Define custom helpers directly on page!!!
app/helpers/articles_helper.rb
16
That’s more like it!!!
app/controllers/articles_controller.rb
17
Page “namespace” is already reserved
app/controllers/articles_controller.rb
Oops
18
app/controllers/articles_controller.rb
JavaScriptProxy or custom helper?!
19
public/javascripts/application.js
Maintenance headache
app/helpers/articles_helper.rb
app/controllers/articles_controller.rb
20
app/controllers/articles_controller.rb
Method missing abuse
Oops
21
What now?
22
Let’s look back to the origins of RJS
23
Initial API for Ajax in Rails
24
Indicates which element to update
25
Faced with a tricky problem
26
:update won’t cut it
27
Wouldn’t it be nice if you could...?
28
Thanks Ruby :-)
29
Initial API
30
Ugly. Not quite right.
Oops
31
Wrap it up in a new template type
actionpack/lib/action_view/base.rb
32
Just update_page under the sheets
actionpack/lib/action_view/base.rb
33
update_page just spits out JavaScript
34
<< just adds raw JavaScript to the stream
is the same as
35
So...36
Wrap update_page in a helper
app/helpers/articles_helper.rb
37
Reuse it with <<
app/controllers/articles_controller.rb
38
Bonus
39
Reuse it client side
40
Having said all that...
41
No use obviates reuse
app/helpers/application_helper.rb public/stylesheets/screen.css
app/views/articles/edit.rhtml
42
Questions, comments & etc
43
Questions, comments & etc
Thanks44