I gave this talk Feb 13th, 2013 at the Atlanta Ruby Meetup - covering Rails + Backbone.js
In this presentation I'll cover how well Ruby on Rails + Backbone work together. There are a lot of misconceptions when moving logic to the client, and how that changes the backend.
Rails has a lot of tools that make this a breeze, and with the right setup, working with javascript templates, using Rails view helpers, and generating JSON is super simple.
Rails comes with everything you need to deploy to production, minify, concatenate, and obfuscate all your Javascript files, and even load them in the proper order via the Asset Pipeline.
See videos and screencasts at:
http://www.backbonerails.com
3. Looking Back
• Came from MVC PHP :-(
before 2011
‣ CodeIgniter
• Built internal CRM’s
• Used jQuery / AJAX for front end logic
‣ Spaghetti code
‣ One giant file
‣ Perplexing... never felt right
• Started Ruby on Rails - late 2011
Backbone + Rails: Journey to the Front End
17. What solves this problem?
• + Give logical structure to the DOM
• + Separate behavior, presentation, and implementation
• + Automatically keep view changes in sync
• + Prevent jQuery spaghetti code callbacks
Backbone + Rails: Journey to the Front End
18. What solves this problem?
• + Give logical structure to the DOM
• + Separate behavior, presentation, and implementation
• + Automatically keep view changes in sync
• + Prevent jQuery spaghetti code callbacks
= Javascript MVC Frameworks
Backbone + Rails: Journey to the Front End
19. Difficult Questions
• What’s the best Framework?
• What are the differences between...
‣ Backbone JS
Jan 2012
‣ Ember JS
‣ Knockout JS
‣ ... Spine ... Batman ... Angular ...
Backbone + Rails: Journey to the Front End
20. Why I Chose Backbone
• Highly successful production-ready track record
‣ Major players using it successfully
• Non-intimidating documentation
• Seemed to be built for Rails out of the box
‣ REST API - No Configuration
• Awesome community support
‣ Lots of tutorials
‣ Stack Overflow
Backbone + Rails: Journey to the Front End
21. How Backbone Works
• Introduces the concepts of Client Side MVC
‣ View - managing particular DOM node (template)
‣ Models / Collections - storing state of entities
‣ Router - Interprets client side URL / fires events
‣ Events - communication mechanism between components
• The Ramifications
‣ No more mixed presentation and behavior
‣ You’re in control of everything, all interactions
‣ Enough basic structure to build widgets / small SPA’s
Backbone + Rails: Journey to the Front End
32. Day
where: Dentist,
date: 02-02-2013,
-February-
time_start: 2:00pm,
time_end: 3:00pm M T W TH F S SU
},{
id: 2,
name: Ruby Meetup,
1 2 3 4 5 6 7
where: Centergy Bld,
Doctors
46. Day
where: Dentist,
date: 02-02-2013,
-February-
time_start: 2:00pm,
time_end: 3:00pm M T W TH F S SU
},{
id: 2,
name: Ruby Meetup,
1 2 3 4 5 6 7
where: Centergy Bld,
Doctors
60. Day
where: Dentist,
date: 02-02-2013,
-February-
time_start: 2:00pm,
time_end: 3:00pm M T W TH F S SU
},{
id: 2,
name: Ruby Meetup,
1 2 3 4 5 6 7
where: Centergy Bld,
Doctors
74. Day
where: Dentist,
date: 02-02-2013,
-February-
time_start: 2:00pm,
time_end: 3:00pm M T W TH F S SU
},{
id: 2,
name: Ruby Meetup,
1 2 3 4 5 6 7
where: Centergy Bld,
Doctors
88. Day
where: Dentist,
date: 02-02-2013,
-February-
time_start: 2:00pm,
time_end: 3:00pm M T W TH F S SU
},{
id: 2,
name: Ruby Meetup,
1 2 3 4 5 6 7
where: Centergy Bld,
Doctors
102. Day
where: Dentist,
date: 02-02-2013,
-February-
time_start: 2:00pm,
time_end: 3:00pm M T W TH F S SU
},{
id: 2,
name: Ruby Meetup,
1 2 3 4 5 6 7
where: Centergy Bld,
Doctors
116. Day
where: Dentist,
date: 02-02-2013,
-February-
time_start: 2:00pm,
time_end: 3:00pm M T W TH F S SU
},{
id: 2,
name: Ruby Meetup,
1 2 3 4 5 6 7
where: Centergy Bld,
Doctors
302. Server
M C
V
div id=header-region/div
div id=main-region/div
JS
ON
div id=footer-region/div
ON
JS
%= javascript_tag do %
$(function() {
App.start();
});
% end %
V
Client
M C
304. Do you still need Rails?
• Asset Pipeline !
• No more awesome helpers right?
‣ wrong.
• Routes.rb / I18n / Controller Instance Vars
• Precompiled JST
• ActiveRecord / Controllers / Gems
Backbone + Rails: Journey to the Front End
309. {
id: 1,
picture: images/user_1.png,
div id=”user”Hello, Stanley Kubrick/div age: 70,
name: Stanley Kubrick,
gender: male,
company: MGM,
phone: 832-547-3983,
email: stanley.kubrick@mgm.com,
address: Hertfordshire England,
friends: [
{
id: 1,
name: Malcolm McDowell
},
{
id: 2,
name: Arthur C. Clarke
},
{
id: 3,
name: Peter Sellers
}
]
}
310. {
id: 1,
picture: images/user_1.png,
div id=”user”Hello, Stanley Kubrick/div age: 70,
name: Stanley Kubrick,
gender: male,
company: MGM,
phone: 832-547-3983,
email: stanley.kubrick@mgm.com,
a href=”mailto:stanley.kubrick@mgm.com”
address: Hertfordshire England,
Email Stanley
friends: [
/a {
id: 1,
name: Malcolm McDowell
},
{
id: 2,
name: Arthur C. Clarke
},
{
id: 3,
name: Peter Sellers
}
]
}
311. {
id: 1,
picture: images/user_1.png,
div id=”user”Hello, Stanley Kubrick/div age: 70,
name: Stanley Kubrick,
gender: male,
company: MGM,
phone: 832-547-3983,
email: stanley.kubrick@mgm.com,
a href=”mailto:stanley.kubrick@mgm.com”
address: Hertfordshire England,
Email Stanley
friends: [
/a {
id: 1,
name: Malcolm McDowell
},
div id=”friends” {
span id=”count”You have 3 friends:/span id: 2,
ul name: Arthur C. Clarke
liMalcolm McDowell/li },
liArthur C. Clarke/li {
liPeter Sellers/li id: 3,
/ul name: Peter Sellers
/div }
]
}
312. Rabl https://github.com/nesquena/rabl
## models/user.rb
class User ActiveRecord::Base
has_many :posts
attr_accessible :first_name, :last_name
def full_name
#{first_name} #{last_name}
end
end
313. Rabl https://github.com/nesquena/rabl
## models/user.rb
class User ActiveRecord::Base
has_many :posts
attr_accessible :first_name, :last_name
def full_name
#{first_name} #{last_name}
end
## views/users/index.json.rabl
end
collection @users
attributes :id, :first_name, :last_name, :full_name, :date_created
node do |user|
{
:date_created_formatted = user.date_created.to_s(:db),
:date_created_ago = time_ago_in_words(user.date_created)
}
end
child :posts do
attributes :id, :title, :body
end
314. Rabl [{
id: 1,
https://github.com/nesquena/rabl
first_name: Brian,
last_name: Mann,
## models/user.rb full_name: Brian Mann,
class User ActiveRecord::Base 2013-02-02T18:35:35.511,
date_created:
has_many :posts date_created_formatted: 2013-02-02 1:35:35,
date_created_ago: about 2 weeks,
posts: [{
attr_accessible :first_name, :last_name
id: 100,
title: BackboneRails Released!,
def full_name body: go download them.,
#{first_name}}]#{last_name}
end },{
## views/users/index.json.rabl
id: 2,
end first_name: Jennifer,
last_name: Shehane,
collection @users
full_name: Jennifer Shehane,
date_created: 2013-02-10T22:13:46.245,
attributes :id, :first_name,2013-02-10 5:13:46,
date_created_formatted: :last_name, :full_name, :date_created
date_created_ago: 3 days,
nodeposts: [{
do |user|
{ id: 101,
:date_created_formatted = user.date_created.to_s(:db),
title: You misspelled ‘tenants’ in Ep02,
:date_created_ago ‘tenets’,
body: should be = time_ago_in_words(user.date_created)
} },{
id: 102,
end
title: Uhhh.....,
body: It is misspelled on every single slide :D,
child :posts do
}]
attributes :id, :title, :body
}]
end
328. ## users/templates/show.jst.eco
%- @linkTo Routes.user_path(@id), Class: button button-mini, - %
i class='icons-user'/i Show User
% end %
## users/show.html.erb
%= link_to user_path(@user), class: button button-mini do %
i class='icons-user'/i Show User
% end %
329. ## users/templates/show.jst.eco
%- @linkTo Routes.user_path(@id), Class: button button-mini, - %
i class='icons-user'/i Show User
% end %
## users/show.html.erb
%= link_to user_path(@user), class: button button-mini do %
i class='icons-user'/i Show User
% end %
!--
a href=users/1 class=button button-mini
i class='icons-user'/i Show User
/a
--
331. Technical Difficulties
• Huge mental shi / nuanced decisions
‣ When / how to load server resources
‣ Handling Routing w/Dialogs
‣ Complex nested layouts
• Lack of Application Infrastructure
May 2012
‣ Slow, Painful Development
‣ Bug Recursions
‣ Edge Cases
‣ Excess Boilerplate
Backbone + Rails: Journey to the Front End