Assuming each A has 0 or more B and each B has 0 or more C items. How would one present this relationship in Backbone.js (v0.5). I would like to have urls like /A/a/B/b/C/c. The following code is in coffeescript. I am confused, how to set the url variables in the follow code.
class As extends Backbone.Collection
url: "/A"
model: A
class A extends Backbone.Model
initialize: ->
@Bs = new Bs()
@Bs.parent=@
@Bs.url="/A/#{@id}"
class Bs extends Backbone.Collection
model: B
class B extends Backbone.Model
initialize: ->
@Cs = new Cs()
@Cs.url = ??? # => /A/a/B/b/C/
@Cs.parent=@
class Cs extends Backbone.Collection
model: C
class C extends Backbone.Model
url: #?? url => /A/a/B/b/C/c
I would not create nested URL’s unless you really have a good case for it. Each resource can be defined by a resource name and an id. The relationship between the objects is internal.
That being said lots of hits to the server to pull out nested objects is not really ideal. You are better having a single resource that returns nested json
For example the data I get back from my server looks like
The sub nodes are assembled by a custom controller that takes the individual db records and makes a tree of data.
However you are now in trouble because backbone.js natively only supports flat structures. I’ve made some modifications to the basic Backbone.Model to support processing tree like structures.
I’ll paste it here if it might be of use to you.
You can use it like
The last line will trigger an event “change:excercise/duration”