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
class Person extends Backbone.RelationalModel
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".
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?
class X extends Y
There's three possible workaround that I can see:
X = Backbone.RelationalModel.extend
class X extends Backbone.RelationalModel
class X extends Backbone.RelationalModel; new X
I'll keep this open until I've noted these in the docs.
Add tutorial link (fixes #77); explain issues with CoffeeScript inher…
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.Relational.store.getCollection( this.instance )
Is not defined.
Solution: Call MyNewModel.setup() after creating MyNewModel extending Backbone.RelationalModel.
after creating or after defining?