Guest User

Untitled

a guest
Apr 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $().ready ->
  2.   window.App = {} # instances
  3.  
  4.   ### Models  ###
  5.   class window.Stop             extends Backbone.RelationalModel
  6.     url: -> @get('url')
  7.     relations: [{
  8.       type: Backbone.HasOne
  9.       key: 'direction'
  10.       relatedModel: 'Direction'
  11.       collectionType: 'DirectionList'
  12.       reverseRelation: {
  13.         key: 'stops'
  14.       }
  15.     }]
  16.  
  17.   class window.Direction        extends Backbone.RelationalModel
  18.     url: -> @get('url')
  19.     relations: [{
  20.       type: Backbone.HasOne
  21.       key: 'route'
  22.       relatedModel: 'Route'
  23.       collectionType: 'RouteList'
  24.       reverseRelation: {
  25.         key: 'directions'
  26.       }
  27.     }, {
  28.       type: Backbone.HasMany
  29.       key: 'stops'
  30.       relatedModel: 'Stop'
  31.       collectionType: 'StopList'
  32.       reverseRelation: {
  33.         key: 'direction'
  34.       }
  35.     }]
  36.  
  37.   class window.Route            extends Backbone.RelationalModel
  38.     url: -> @get('url')
  39.     relations: [{
  40.       type: Backbone.HasOne
  41.       key: 'agency'
  42.       relatedModel: 'Agency'
  43.       collectionType: 'AgencyList'
  44.       reverseRelation: {
  45.         key: 'routes'
  46.       }
  47.     }, {
  48.       type: Backbone.HasMany
  49.       key: 'directions'
  50.       relatedModel: Direction
  51.       collectionType: 'DirectionList'
  52.       reverseRelation: {
  53.         key: 'route'
  54.       }
  55.     }]
  56.  
  57.   class window.Agency           extends Backbone.RelationalModel
  58.     url: -> @get('url')
  59.     relations: [{
  60.       type: Backbone.HasMany
  61.       key: 'routes'
  62.       relatedModel: Route
  63.       collectionType: 'RouteList'
  64.       reverseRelation: {
  65.         key: 'agency'
  66.       }
  67.     }]
  68.  
  69.   class window.StopList         extends Backbone.Collection
  70.     model: Stop
  71.     url: -> "#{@get('direction').url()}/stops"
  72.  
  73.   class window.DirectionList    extends Backbone.Collection
  74.     model: Direction
  75.     url: -> "#{@get('route').url()}/directions"
  76.  
  77.   class window.RouteList        extends Backbone.Collection
  78.     model: Route
  79.     url: -> "#{@get('agency').url()}/routes"
  80.  
  81.   class window.AgencyList       extends Backbone.Collection
  82.     model: Agency
  83.     url: -> "/agencies"
  84.  
  85.   App.agencies = new AgencyList
  86.  
  87.   # setup
  88.   App.agencies.fetch()
  89.   a1 = App.agencies.first()
  90.   a1.fetchRelated()
  91.   rs = a.get('routes')
  92.  
  93.   # the issue
  94.   a2 = rs.get('agency')
  95.   # a2 should equal a1, but instead, a2 is undefined
  96.   # help?
Add Comment
Please, Sign In to add comment