Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is exactly what it feels like to build Backbone interfaces!

Definitely would give someone a leg up learning for the first time, compared to the Todos app. Plus it doesn't have anti-patterns like `this.model.view = this`, a line which Yehuda Katz actually mocked at a recent SproutCore meetup.



Out of curiosity, what did Mr. Katz say about setting a reference from the model to the view at the meetup?


He had led in to a section on "Other MVC Frameworks". Then a single slide with some code along these lines:

  Todo = OtherMVC.Model.extend({
    clear: function() { this.view.remove(); }
  });

  TodoView = OtherMVC.View.extend({
    initialize: function() { this.model.view = this; }
  });
Basically he ragged on that for breaking MVC, and never mentioned Backbone by name. I spoke to him afterwards, and he seemed pretty set against Backbone. He considers manually bubbling events to be tedious: "not the happy path".

I've been inspired by Morphic recently, where models having knowledge of their views makes sense. When I was a beginner though, I was briefly puzzled by how to connect multiple views to a model and vice versa: I first tried to extend the pattern above before binding to model events.


I thought loose coupling was a feature.


lol! I saw the same at another recent meetup. Damn, Katz really doesn't like Backbone...


As someone who's just getting started with Backbone, I'd be very interested to see that talk, and here suggestions for what to do instead of 'this.model.view = this'.


If Spine is anything to go by, you bind events from the view to the model and then trigger them from your code on the model instance.


Usually that's the case -- but if you know for certain that there's a one-to-one relationship between a model and it's view, then there's no harm in having a direct reference for convenience. Pragmatism > Purity, where UI code is concerned.


I agree with you. I'm concerned about memory leaks from the circular references, though.


Circular references are garbage collected -- no need to worry about them.

You might be thinking of a terrible old IE bug where circular references between JavaScript objects and DOM nodes were never collected, but that's never been an issue with JavaScript-only references.


Oh, I didn't know that. That alleviates many of my concerns, then, thank you.


The Responder pattern used in SproutCore (inspired by Cocoa) is very elegant.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: