Professional Documents
Culture Documents
?
y
l
l
a
Re
Programming in the
Reactive Style using
Meteor JS
Dave Anderson
dave@neo.com @eymiha
Why Meteor?
Three Virtues
of a Great
Web
Developer
Programmer
Laziness
Impatience
Hubris
Laziness
Impatience
Hubris
What Matters?
Fastest to Goal
Most for Least Effort
It Just Works
What Obstructs?
Traditional Wide
Web Technology Stack
Client:
HTML, CSS, JavaScript
!
Server:
Programming Language, Database Access,
Marshaling Language
Meteor
Web Technology Stack
Client:
HTML, CSS, JavaScript, MiniMongo
!
Server:
JavaScript, Mongo
Additional Stack
Java
Ruby on Rails
ASP
Controls, Extensions
Clojure
Clojure files
Meteor
packages
...repeatedly
Extra Coding
This one is hard to quantify.
HTML with handlebar templates
JavaScript / CoffeeScript
CSS / SCSS
packages
the code needed to build
a Meteor-based web UX
is the most straightforward Ive found.
YMMV
Conventional Flow
Code uses the current values when called.
If a is 5 and b is 3, then
c = a+b
would set c to 8.
Later, if a became 4 and b became 2,
then c would still be 8 - until the code
was called again.
Reactive Flow
Code uses values as they change.
If a is 5 and b is 3, then
c = a+b
would set c to 8.
Later, if a became 4 and b became 2,
then c would become 6 automatically.
webu13attend.meteor.com
webu13attend Deconstruction
client-only code
shared code
webby bits
server-only code
handlebars!
client/webu13attend.html
client/webu13attend.html
client/webu13attend.html
client/webu13attend.html
client/webu13attend.scss
client/webu13attend.coffee
server/attendees.coffee
lib/collections.coffee
public/images/webu13.png
A Closer Look at
Reactivity
Imperative Coding
if youre lucky enough to have database notification
through an observe-like function,
you implement callbacks
Items.find().observe!
added: (item) ->!
$('ul').append '<li id="'+item._id+'">'+!
item.name+'</li>'!
changed: (item) ->!
$('ul li#'+item._id).text item.name!
removed: (item) -> !
$('ul li#'+item._id).detach()
Declarative Coding
Meteor does the observe callback behind the scenes
<template name="itemList">!
<ul>!
{{#each items}}!
<li>{{name}}</li>!
{{/each}}!
</ul>!
</template>
Template.itemList.helpers!
items: -> Items.find()
Reactive Source
first use
Reactive Source
Computation Processing
Reactive Source
Dependency Established
Reactive Source
ready to go
Why Computations?
Because Meteor has the potential to
change the entire interface
when a reactive source changes!
Changes based on a computation becoming invalid
focus on only the parts of the interface
that depend on the computation.
Reactive Source
ready to go
Reactive Source
Source Updated
Reactive Source
Computation Invalidated
Reactive Source
Computation Processing
Reactive Source
Computation Processing
Reactive Source
ready to go
local (client)
database queries
(subscriptions)
session variables
status
user / user id
logging in
subscription ready
Deps
Computation
Dependency
Deps.Computation
firstRun
onInvalidate(function)
invalidate( )
invalidated
stop( )
stopped
Deps.Dependency
depend( )
changed( )
hasDependents( )
Deps
autorun(function)
currentComputation
active
onInvalidate(function)
flush( )
afterFlush(function)
nonreactive(function)
An Example:
Injectives
Sessions variables are reactive sources
global to the client.
An Injective is a local reactive source that
can be more closely scoped to the
functions and objects that uses it.
Example of Injectives
Use the height and width of a browser window
to control the size of some objects.
innerWidth = Deps.injective window.innerWidth!
innerHeight = Deps.injective window.innerHeight!
!
$(window).resize ->!
innerWidth.set window.innerWidth!
innerHeight.set window.innerHeight!
!
Template.box.helpers!
size: ->!
Math.floor (innerWidth.get()+innerHeight.get())/4.0
So why should
Meteor
and
Reactivity
Narrower
technology
stack
to find and
change code
to me?
Easiermatter
Try It!
e
h
t
e
e
k
g
n
Ta
e
l
l
a
h
C
r
o
e
t
e
M
Questions?
Dave Anderson
dave@neo.com @eymiha