Backbone.js Routers

Backbone.js Routers The Rails way Remove the 'Hello From Backbone' alert from our ScratchPad initialize method in app/assets/javascripts/scratch_pad.js.coffee Create a Rails controller NotesController class NotesController This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/backbone-js-routers

What is the benefit of naming instance variables this way…and does this sort of variable have a name?

def notes
  @_notes ||= Note.all
end

Are you referring to starting the variable with an underscore? I believe this convention it comes from JavaScript, where internal private object attributes are named with an underscore to indicate they are private and shouldn’t be used externally. I’ve seen this convention written in other languages lately. So here I’d see this as saying don’t use the actual instance variable “@_notes”, just refer to the “notes” accessor method instead. If you notice in the video in the index view, he just refers to “notes” for the li iteration block.

Yes, that is what I was referring to. Thanks for the answer!

That’s a pattern that is actually used for caching a return value, the _ prefix does mean we don’t care about the name of the variable, but that’s just a little bit of added semantics.

That makes sense, thanks

Just to confirm: routing to /notes/:id was a typo, and we should always eliminate the leading slash in Backbone routes, yes?

@Tambling I thought the same, but watching the video further he shows the setting the Backbone.history.start( pushState: true ) allows the forward slash to stay: /notes/:id
but just noticed you do have to remove the / in the code, kinda confusing, really have to pay attention to what he’s typing