Want to see the full-length video right now for free?
Wrap each line of AllNotes with new Backbone.Model
Models in Backbone don't care about your schema, you can set any attribute on any model. Because of this, we don't even need to create a separate class if we only want a dumb data container
window.ScratchPad = Models: {} Collections: {} Views: {} Routers: {} initialize: -> @AllNotes = [ new Backbone.Model(id: 1, title: 'make a second todo item', complete: true), new Backbone.Model(id: 2, title: 'Make another todo list', complete: false), new Backbone.Model(id: 3, title: 'DSICO PARTY!', complete: false) ] new @Routers.ScratchPadRouter Backbone.history.start(pushState: true)
Note that our views have all broken
Note that we can leave id alone
<ul>
<% for note in @notes: %>
<li>
<dl>
<dt>Title</dt>
<dd><a href="/notes/<%= note.id %>"><%= note.get('title') %></a></dd>
<dt>Content</dt>
<dd><%= note.get('content') %></dd>
</dl>
</li>
<% end %>
</ul>
We could also have passed the plain old Javascript object to the view, accessed things via
@title
, and only had to change the render method to
@template(@model.attributes)
It's a style preference
Some people prefer to do @template(@model.toJSON())
instead of attributes. I do not ever use toJSON
in this fashion, so I can reserve it for translating between my client side and server side domain, more on that later.
We need to make the same changes to our edit.jst.eco template
<%= @note.get('content') %>
Change setters in our EditNote Backbone view to use the .set method
We have to make this change for the same reason we use get above
saveModel: (e) ->
@model.set
title: @$('.title').val()
content: @$('.content').val()
Backbone.history.navigate('/', trigger: true)
false
Change href to call note.url()
<ul>
<% for note in @notes: %>
<li>
<dl>
<dt>Title</dt>
<dd><a href="<%= note.url() %>"><%= note.get('title') %></a></dd>
<dt>Content</dt>
<dd><%= note.get('content') %></dd>
</dl>
</li>
<% end %>
</ul>
To do this, we must create a Note
Backbone model.
class App.Models.Note extend Backbone.Model
urlRoot: '/notes'
Finally we'll need to update our ScratchPad
app
window.ScratchPad =
Models: {}
Collections: {}
Views: {}
Routers: {}
initialize: ->
@AllNotes = [
new @Models.Note(id: 1, title: 'make a second todo item', complete: true),
new @Models.Note(id: 2, title: 'Make another todo list', complete: false),
new @Models.Note(id: 3, title: 'DSICO PARTY!', complete: false)
]
new @Routers.ScratchPadRouter
Backbone.history.start(pushState: true)
Commit!