0

Having seen it in a couple of tutorials, I'm trying to execute the following line of code in my view

@model.on('change', @render, this)

Unfortunately the change event is not firing and therefore my view is not re-rendering.

I've tried binding to different events and creating a couple of custom events using the trigger function but nothing seems to be firing at all on the model. Furthermore, there are no errors coming from the console. The change event is working fine on a different collection. I'm using Zepto 1.0, Backbone.js 0.9.2 and Underscore.js 1.3.1

EDIT: I'm trying to execute the following from the Router

  place: (id) ->
    @model = new GM.Models.Place({id: "#{id}"})
    @model.fetch
    view = new GM.Views.Place(model: @model)
    $('#container').html(view.render().el)

And my model is set up like this:

class GM.Models.Place extends Backbone.Model
  urlRoot: '/mobile/place'

I am wondering if anyone has experienced similar problems before and has a quick fix. If not and you need more of the code to find an explanation please let me know...

socratic_singh
  • 183
  • 1
  • 13

1 Answers1

1

You're not actually calling the @model.fetch method anywhere. This:

@model.fetch

is not a method call, you need to add parentheses or arguments if you want to call the method:

@model.fetch()
# or
@model.fetch success: -> ...
# etc.

Otherwise you're just producing this.model.fetch; in the JavaScript and that doesn't do anything useful.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Thanks! Can't believe this - rookie mistake, still getting used to Coffeescript...! – socratic_singh Apr 23 '12 at 19:58
  • 1
    @socratic_singh: A lot of people (including me) always include the parentheses just to avoid having to worry about when you need them and when you don't. – mu is too short Apr 23 '12 at 20:01
  • Will definitely be doing that from hereonin. Thanks again – socratic_singh Apr 25 '12 at 13:14
  • 1
    @socratic_singh: They can also help with grouping [multi-line function calls](http://coffeescript.org/#try:some_function(%0A%20%20%20%20a%3A%20'a'%0A%20%20%20%20b%3A%20'b'%0A%20%20%20%20c%3A%20-%3E%0A%20%20%20%20%20%20%20%20blahblah()%3B%0A)%0Asomething_else('pancakes')) to make the structure easier to see; I find the indentation-based structure difficult to grok at a glance when things get nested (especially with the 2 character indent that many people insist on using). – mu is too short Apr 25 '12 at 18:18