Marcel Molina Jr. Presentation

44
Sharing RJS: Reuse at the app level Marcel Molina Jr. 37signals 1

description

 

Transcript of Marcel Molina Jr. Presentation

Page 1: Marcel Molina Jr. Presentation

Sharing RJS: Reuse at the app level

Marcel Molina Jr.37signals

1

Page 2: Marcel Molina Jr. Presentation

A little bit about me

2

Page 3: Marcel Molina Jr. Presentation

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

Page 4: Marcel Molina Jr. Presentation

Work for 37signals

4

Page 5: Marcel Molina Jr. Presentation

Hottest Hacker On Earth

David Has And Belongs To Many Hanssons

5

Page 6: Marcel Molina Jr. Presentation

Sharing RJS

6

Page 7: Marcel Molina Jr. Presentation

Typical RJS

app/controllers/articles_controller.rb

7

Page 8: Marcel Molina Jr. Presentation

app/controllers/articles_controller.rb

Duplication

Oops

8

Page 9: Marcel Molina Jr. Presentation

Let’s fix this

9

Page 10: Marcel Molina Jr. Presentation

Extract to private method of controller

app/controllers/articles_controller.rb

10

Page 11: Marcel Molina Jr. Presentation

app/controllers/articles_controller.rb

Wrong scope!

Oops

11

Page 12: Marcel Molina Jr. Presentation

Extract to helper, passing page

app/helpers/articles_helper.rb

12

Page 13: Marcel Molina Jr. Presentation

This works

app/controllers/articles_controller.rb

13

Page 14: Marcel Molina Jr. Presentation

But it’s Pythonic :-(

Oops

app/controllers/articles_controller.rb

14

Page 15: Marcel Molina Jr. Presentation

Monkey patch JavaScriptGenerator!!!

15

Page 16: Marcel Molina Jr. Presentation

Define custom helpers directly on page!!!

app/helpers/articles_helper.rb

16

Page 17: Marcel Molina Jr. Presentation

That’s more like it!!!

app/controllers/articles_controller.rb

17

Page 18: Marcel Molina Jr. Presentation

Page “namespace” is already reserved

app/controllers/articles_controller.rb

Oops

18

Page 19: Marcel Molina Jr. Presentation

app/controllers/articles_controller.rb

JavaScriptProxy or custom helper?!

19

Page 20: Marcel Molina Jr. Presentation

public/javascripts/application.js

Maintenance headache

app/helpers/articles_helper.rb

app/controllers/articles_controller.rb

20

Page 21: Marcel Molina Jr. Presentation

app/controllers/articles_controller.rb

Method missing abuse

Oops

21

Page 22: Marcel Molina Jr. Presentation

What now?

22

Page 23: Marcel Molina Jr. Presentation

Let’s look back to the origins of RJS

23

Page 24: Marcel Molina Jr. Presentation

Initial API for Ajax in Rails

24

Page 25: Marcel Molina Jr. Presentation

Indicates which element to update

25

Page 26: Marcel Molina Jr. Presentation

Faced with a tricky problem

26

Page 27: Marcel Molina Jr. Presentation

:update won’t cut it

27

Page 28: Marcel Molina Jr. Presentation

Wouldn’t it be nice if you could...?

28

Page 29: Marcel Molina Jr. Presentation

Thanks Ruby :-)

29

Page 30: Marcel Molina Jr. Presentation

Initial API

30

Page 31: Marcel Molina Jr. Presentation

Ugly. Not quite right.

Oops

31

Page 32: Marcel Molina Jr. Presentation

Wrap it up in a new template type

actionpack/lib/action_view/base.rb

32

Page 33: Marcel Molina Jr. Presentation

Just update_page under the sheets

actionpack/lib/action_view/base.rb

33

Page 34: Marcel Molina Jr. Presentation

update_page just spits out JavaScript

34

Page 35: Marcel Molina Jr. Presentation

<< just adds raw JavaScript to the stream

is the same as

35

Page 36: Marcel Molina Jr. Presentation

So...36

Page 37: Marcel Molina Jr. Presentation

Wrap update_page in a helper

app/helpers/articles_helper.rb

37

Page 38: Marcel Molina Jr. Presentation

Reuse it with <<

app/controllers/articles_controller.rb

38

Page 39: Marcel Molina Jr. Presentation

Bonus

39

Page 40: Marcel Molina Jr. Presentation

Reuse it client side

40

Page 41: Marcel Molina Jr. Presentation

Having said all that...

41

Page 42: Marcel Molina Jr. Presentation

No use obviates reuse

app/helpers/application_helper.rb public/stylesheets/screen.css

app/views/articles/edit.rhtml

42

Page 43: Marcel Molina Jr. Presentation

Questions, comments & etc

43

Page 44: Marcel Molina Jr. Presentation

Questions, comments & etc

Thanks44