Doesn't play nice with CoffeeScript class/extends syntax. #91

Closed
DouweM opened this Issue Mar 20, 2012 · 5 comments

4 participants

@DouweM
Collaborator

When using Backbone-relational with CoffeeScript, creating a Backbone.RelationalModel subclass using the CoffeeScript class/extends syntax doesn't work properly, as the reverse relation initialization in Backbone.RelationalModel.extend() is never called. Note that this syntax does work when subclassing Backbone.Model, as this doesn't have any special logic in its .extend() method.

CoffeeScript users currently have to use:

Person = Backbone.RelationalModel.extend
  relations: [
    # etc.
  ]

instead of:

class Person extends Backbone.RelationalModel
  relations: [
    # etc.
  ]

Losing the slightly nicer syntax isn't so bad, weren't it for the fact that this means you can't use cool CoffeeScript things like super and the @static: value syntax for setting static variables either.

This isn't a problem with Backbone-relational per se, but it would be nice to find a way around this problem so that CoffeeScript users will be able to use the combination of Backbone-relational and CoffeeScript to the fullest extent. This issue is here so that hopefully someone will come up with a nice solution. Maybe it'll be me, but I thought I'd share it here with the community so that others are aware of this not working and can think about a solution as well.

In the mean time, I think it'd be a good idea to place a Note in the README pointing out that the CoffeeScript syntax doesn't work yet, so that people won't get a nasty surprise when their reverse relations aren't working properly. There are a lot of people that could be having problems with this, seeing how many people are using Backbone.js with CoffeeScript: Google search for "backbone coffeescript".

@PaulUithol
Owner

Ah, thanks.. that's not very nice :\. I just removed the section from the readme explaining how to work around this.

It sort of looks like using the Coffeescript class X extends Y syntax bypasses the Backbone extend mechanism completely, generating code that's quite close to what Backbone would normally do in extend?

There's three possible workaround that I can see:

  • use the X = Backbone.RelationalModel.extend syntax instead of Coffeescript's class X extends Backbone.RelationalModel
  • don't use reverseRelations
  • initialize a blank placeholder model right after defining a model that contains reverseRelations; this will also bootstrap the relations, for example: class X extends Backbone.RelationalModel; new X

I'll keep this open until I've noted these in the docs.

@DouweM
Collaborator
@mattiassvedhem

Iv'e tried using the workaround where I define a class and then create an empty placeholder but I get this error:

Cannot call method 'bind' of undefined
Backbone.Relationbackbone.relational.js:323

Where

Backbone.Relational.store.getCollection( this.instance )

Is not defined.

@DouweM
Collaborator

Solution: Call MyNewModel.setup() after creating MyNewModel extending Backbone.RelationalModel.

@DouweM DouweM closed this May 16, 2012
@elgubenis

after creating or after defining?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment