Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DouweM opened this issue Mar 20, 2012 · 5 comments · Fixed by #112
Closed

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

DouweM opened this issue Mar 20, 2012 · 5 comments · Fixed by #112

Comments

@DouweM
Copy link
Collaborator

DouweM commented Mar 20, 2012

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
Copy link
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
Copy link
Collaborator Author

DouweM commented Mar 25, 2012

–Douwe

On 25 mrt. 2012, at 23:02, Paul [email protected] wrote:

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?

Yup, that's exactly what's happening.

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

This is certainly a solution, but as mentioned this would kill a big part of why I (and presumably others use CoffeeScript: the nice features available when subclassing, like easily calling super.

  • don't use reverseRelations

Would work, but far from ideal. The reverseRelation property is there for a reason after all.

  • initialize a blank placeholder model right after defining it for any model defining
    reverseRelations; this will also bootstrap the relations, for example: X = Backbone.RelationalModel.extend; new X

I think this is what I'll be going with till I think of a better way to hook into the CoffeeScript class definition process.

But I think there's one thing we're forgetting: the overridden extend method doesn't just initialize the reverseRelations, it also sets the model option fir all of the new relation's to the newly defined subclass, which is very important when using subclasses of subclasses of Backbone.RelationalModel. I guess I would have to do setting of model manually.

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

Don't you think it'd be good to keep this open even after that? I'm still kind of hopeful someone will come up with a way to actually fix in some clever way, rather than add a notice, use a workaround and be done with it.


Reply to this email directly or view it on GitHub:
#91 (comment)

@mattiassvedhem
Copy link

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
Copy link
Collaborator Author

DouweM commented May 16, 2012

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

@linus-amg
Copy link

after creating or after defining?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants