Skip to content

_.bindAll @ #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 01/script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jQuery ->
initialize: ->
# We're using [Underscore.js's bindAll method](http://documentcloud.github.com/underscore/#bindAll)
# to bind all the view's methods to this instance of our view.
_.bindAll @
_.bindAll @, 'render'
@render()

# `render()` renders the view in `@el`. It must be called by the us;
Expand Down
2 changes: 1 addition & 1 deletion 02/script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jQuery ->
el: $ 'body'

initialize: ->
_.bindAll @
_.bindAll @, 'render', 'addItem'
@counter = 0
@render()

Expand Down
2 changes: 1 addition & 1 deletion 03/script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jQuery ->
# [`add`](http://documentcloud.github.com/backbone/#Collection-add) event
# to the `appendItem()` method.
initialize: ->
_.bindAll @
_.bindAll @, 'render', 'addItem', 'appendItem'

@collection = new List
@collection.bind 'add', @appendItem
Expand Down
60 changes: 30 additions & 30 deletions 04/script.coffee
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
# ##Using a Dedicated View for a Model

# We added a model and collection in the
# [previous example](/03/docs/script.html), but our main application view
# still held the structure for our model. We'll add a dedicated view,
# `ItemView`, for our `Item` model. [The implementation](../) for this part
# We added a model and collection in the
# [previous example](/03/docs/script.html), but our main application view
# still held the structure for our model. We'll add a dedicated view,
# `ItemView`, for our `Item` model. [The implementation](../) for this part
# looks exactly the same as the [implementation of Part 3](/03/).

#
jQuery ->

class Item extends Backbone.Model

defaults:
part1: 'Hello'
part2: 'Backbone'


class List extends Backbone.Collection

model: Item


# The `ItemView` is now responsible for rendering each `Item`.
class ItemView extends Backbone.View

# `tagName` is used to create our
# [`@el`](http://documentcloud.github.com/backbone/#View-el). You can also
# add `className` and `id` properties, but `tagName` is enough for this
# simple example.
tagName: 'li'

initialize: ->
_.bindAll @
_.bindAll @, 'render'

render: ->
$(@el).html "<span>#{@model.get 'part1'} #{@model.get 'part2'}!</span>"
# Returning `@` is considered a good practice. It let's us chain method
# Returning `@` is considered a good practice. It let's us chain method
# calls (i.e., `item_view.render().el`).
@


class ListView extends Backbone.View

el: $ 'body'

initialize: ->
_.bindAll @
_.bindAll @, 'render', 'addItem', 'appendItem'

@collection = new List
@collection.bind 'add', @appendItem

@counter = 0
@render()

render: ->
$(@el).append '<button>Add List Item</button>'
$(@el).append '<ul></ul>'

addItem: ->
@counter++
item = new Item
item.set part2: "#{item.get 'part2'} #{@counter}"
@collection.add item
# `appendItem()` no longer renders an individual `Item`. Rendering is

# `appendItem()` no longer renders an individual `Item`. Rendering is
# delegated to the `render()` method of each `ItemView` instance.
appendItem: (item) ->
# Instantiate a new `ItemView` using `item` as the `model`.
item_view = new ItemView model: item
$('ul').append item_view.render().el

events: 'click button': 'addItem'


list_view = new ListView

# Onward to [Part 5](/05/docs/script.html)
189 changes: 103 additions & 86 deletions 04/script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading