Backbone the Good Parts
-
Upload
renan-carvalho -
Category
Software
-
view
73 -
download
0
Transcript of Backbone the Good Parts
Renan Carvalho
Blog : rcarvalhojs.com
Github : renancarvalho
Twitter : rcarvalhojs
Carioca
Beer Appreciator
Frustrated musician
Love agile methods
Front end developer at Chute
King of age of empires
Hate who says bad things about JS
U2 fan
https://github.com/blogmv/backbone-browserify
The examples will be based on blogMv project
More info http://blogmv.github.io
What’s the problem?
What’s backbone?
Keep data synchronized
Ajax, XHR? (Impossible to remember the syntax)
Spaghetti code
Complex code
How to test it? :( - TDD? http://www.tddunlocked.com.br/
Manage callbacks
And it’s also…
Javascript library
Based on MV* concept
Lightweight (production version has 6.5kb)
Depends on underscore
Trust me, it’s awesome!
What’s backbone?
When to use?
When to use it?
Complex systems
Good Integration with legacy code
Simple systems
Really performatic system
When you don’t know what kind of “helicopter”
Build single page apps
For complex users interface
Reusable code
If you’re under 18
(Flying lollipop)
Call Backbone.history.start(), to start to track url (hashChanges)
Backbone router
More examples : http://backbonejs.org/#Router
If you’d like to use html5 push state, just use Backbone.history.start({pushState: true})
render() - Convenction function for render view. Be
careful with SRP
el - Dom object, that will be used as the view reference
(default is a <div>)
$el - Jquery object that keeps the reference to el.
initialize() - “Constructor” function for every backbone
class
events - Handle view events like ( click, change…)
View
Backbone view
Model
fetch() - get data from server
save() - post/put data on server
If the model has an id attribute, backbone will perform a put.
get/set - get/set attributes on model
parse - function called before backbone merge the server content with the model.
validate - perform custom validations on model. Fired when save(), set or invoked.
url - server endpoint (api), where model will perform the request
More about backbone models: http://backbonejs.org/#Model
Backbone model
Collection
fetch() - Exactly like model, get data from server.
create() - Create a new model instance within a collection (save on
server).
push() - add a model at the end of the collection
add - add a model into a collection, you can specify the position where
the model will be added ( this.collection.add(new model(), {at : 8}); )
reset() - sets the collection with an array of models.
More about backbone collection: http://backbonejs.org/#Collection
Backbone collection
listenTo - Tell an object to listen to a particular event
on another object.
on - Bind a callback function to an object.
stopListening - Tell an object to stop listening to
events.
Backbone events
Trigger you own event
Backbone events
This way you can unbind the function
to the object using “off"