I’m using backbone with the backbone-rails gem which does its own templating and project structure.
The problem is that it puts 4 different views on one div, so what i’ve done is made another div and now the model, show,edit views are assigned to that other view, basically so i can have a list on the left side of the page and everything else in the middle.
The problem is that i can’t redirect now, so when i update or create a new ‘note’ the list view does not refresh.
List View:
Supernote.Views.Notes ||= {}
class Supernote.Views.Notes.IndexView extends Backbone.View
template: JST["backbone/templates/notes/index"]
initialize: () ->
@options.notes.bind('reset','change', @addAll)
addAll: () =>
@options.notes.each(@addOne)
addOne: (note) =>
view = new Supernote.Views.Notes.NoteView({model : note, collection: @options.notes})
@$("li").append(view.render().el)
render: =>
$(@el).html(@template(notes: @options.notes.toJSON() ))
@addAll()
return this
Edit View:
Supernote.Views.Notes ||= {}
class Supernote.Views.Notes.EditView extends Backbone.View
template : JST["backbone/templates/notes/edit"]
events :
"submit #edit-note" : "update"
update : (e) ->
e.preventDefault()
e.stopPropagation()
@model.save(null,
success : (note) =>
@model = note
window.location.hash = "/#{@model.id}"
)
render : ->
$(@el).html(@template(@model.toJSON() ))
this.$("form").backboneLink(@model)
return this
Events is what you need,
when a model is added to a collection (new note)
it raises the
addevent on the collection itselfso in your collection you can catch that and do something with it.
then you have the ‘add note’ covered, (the exact same you could do for the
resetorremoveevent which handle resetting the collection with a new list of models, and deleting a model from the collection )let’s say you update a note, this should be done with the same event system, though the
changeevent could be used for that.the trick here is, your list view renders not the model elements itself, but the list view creates a modelview for every model. in that modelview (you called it
NoteView) you could do the same process as above,and bind to it’s own model: