4. What can you expect?
• What I am doing?
• Why is CoffeeScript so awesome?
• The era before Backbone;
• Fix browser spaghetti code with Backbone;
• Be professional: with Jasmine BDD;
5. Assumptions
• You know your Javascript;
• and your JQuery;
• You developed for the web before.
47. More awesomeness...
number = -42 if opposite
math =
square: (x) -> x * x
race = (winner, runners...) ->
print winner, runners
alert "I knew it!" if elvis?
squares = (math.square num for num in list)
52. Namespaces are cool
class namespace('views').MyView
new oncast.views.MyView()
@.namespace = (name) ->
@.oncast ||= {}
return @.oncast[name] ||= {} if name
return @.oncast
78. Works nicely with
CoffeeScript
Inheritance.
https://github.com/documentcloud/backbone/blob/master/test/model.coffee
79. Works nicely with
CoffeeScript
Inheritance.
class Car extends Backbone.Model
https://github.com/documentcloud/backbone/blob/master/test/model.coffee
83. Models / Collections
class Reference extends Backbone.Model
urlRoot: "/references"
class References extends Backbone.Collection
model: Reference
url: '/references'
Model Model Model
Collection
84. Fetch Collection
# creates a new collection
references = new References
# fetch data asynchronously
references.fetch()
85. Fetch Collection
# creates a new collection
references = new References
# fetch data asynchronously
references.fetch()
[{
id: 1
JSON name: "Paulo's House",
latitude: '10',
longitude: '15'
}]
88. Model attributes
# get reference with id 1
reference = references.get 1
# get the name property of the model
name = reference.get 'name'
89. Model attributes
# get reference with id 1
reference = references.get 1
# get the name property of the model
name = reference.get 'name'
# change the name property of the model
reference.set name: 'new name'
90. Model attributes
# get reference with id 1
reference = references.get 1
# get the name property of the model
name = reference.get 'name'
# change the name property of the model
reference.set name: 'new name'
# save the model to the server
reference.save()
102. Using a View
# creates a new instance of the view
view = new SaveButtonView()
103. Using a View
# creates a new instance of the view
view = new SaveButtonView()
# render it
view.render()
104. Using a View
# creates a new instance of the view
view = new SaveButtonView()
# render it
view.render()
# append the content of the view on the doc
$('.some-div').html view.el
126. The View will represent
one model/collection
forever!
127. Broken events
# append the view's element to the document
$('.div').append view.el
# remove it
$('.div').empty
# add it again
$('.div').append view.el
View events (click, change) will no longer work.
128. Collection.getOrFetch
class Posts extends Backbone.Collection
url: "/posts"
getOrFetch: (id) ->
if @get(id)
post = @get id
else
post = new Post { id : id }
@add post
post.fetch()
post
http://bennolan.com/2011/06/10/backbone-get-or-fetch.html
131. Lazy fetching
class LazyFetchView extends Backbone.View
render: ->
if @model.isFullyFetched()
# if the data is ready we render it
$(@el).html ...
else
# otherwise we fetch the data
# rendering the loading indicator
@fetch()
return @
136. given a delete button view
when the user clicks the view
then it should delete the model
137. Jasmine:
given a delete button view
when the user clicks the view
then it should delete the model
describe 'DeleteButtonView', ->
context 'when the user clicks the view', =>
it 'should delete the model'
139. describe 'DeleteButton', ->
context 'when the user clicks the view', =>
it 'should delete the model', =>
expect(@model.delete).toHaveBeenCalledOnce()
140. describe 'DeleteButton', ->
context 'when the user clicks the view', =>
it 'should delete the model', =>
expect(@model.delete).toHaveBeenCalledOnce()
http://sinonjs.org/
https://github.com/froots/jasmine-sinon
141. describe 'DeleteButton', ->
beforeEach =>
@model = new Backbone.Model
@deleteButton = new DeleteButton(model: @model)
context 'when the user clicks the view', =>
it 'should delete the model', =>
expect(@model.delete).toHaveBeenCalledOnce()
142. describe 'DeleteButton', ->
beforeEach =>
@model = new Backbone.Model
@deleteButton = new DeleteButton(model: @model)
context 'when the user clicks the view', =>
beforeEach =>
sinon.spy @model, 'delete'
it 'should delete the model', =>
expect(@model.delete).toHaveBeenCalledOnce()
143. describe 'DeleteButton', ->
beforeEach =>
@model = new Backbone.Model
@deleteButton = new DeleteButton(model: @model)
context 'when the user clicks the view', =>
beforeEach =>
sinon.spy @model, 'delete'
@deleteButton.render()
$(@deleteButton.el).click()
it 'should delete the model', =>
expect(@model.delete).toHaveBeenCalledOnce()
144. Run it headless!
Text
http://johnbintz.github.com/jasmine-headless-webkit/