Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6119005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:30:07+00:00 2026-05-23T15:30:07+00:00

Assuming each A has 0 or more B and each B has 0 or

  • 0

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   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:30:08+00:00Added an answer on May 23, 2026 at 3:30 pm

    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.

    http://foo.com/a/:id.json
    http://foo.com/b/:id.json
    http://foo.com/c/:id.json
    

    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

    http://foo.com/a/:id.json
    

    For example the data I get back from my server looks like

    {
      "id": 372,
      "context": "office_work",
      "date_of_entry": "2011-7-05 15:22:00",
      "blood_glucose_measurement": 98,
      "food": {
        "exchanges": "98",
        "list": "burger fries"
      },
      "exercise": {
        "duration": "28",
        "list": "running shopping"
      }
    }
    

    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.

    #= require jquery
    #= require underscore
    #= require backbone
    #
    class RailsModel extends Backbone.Model
    
      initialize: (attributes) ->
        data = {}
    
        for k,v of @attributes
          if v.constructor.name == "Object"
            data[k] = new RailsModel(v)
    
        @set data, {silent: true}
    
      get: (field)->
        val  = @
        first =  true
        for p in field.split('/')
          if first
            val = super(p)
          else
            # This allows returning *undefined* rather
            # than an exception if the parent of a deeply
            # nested node does not exist.
            val = if val? then val.get(p) else undefined
          first = false
        val
    
      # Allow heirarchical setting of objects
      #
      # Example
      #
      # model.set_field('root/child', 10)
      #
      # Will create the path if it does not exist
      set_field: (field, value, silent=false)->
        path = field.split('/')
    
        node = undefined
        val  = @
        for p in field.split('/')
          node = val
          val = node.get(p)
          if not val?
            data = {}
            val = new RailsModel
            data[p] = val
            node.set data
    
        data = {}
        data[p] = value
        node.set data
    
        if not silent and /\//.test(field)
          @trigger("change:#{field}", @model, value)
          @trigger("change", @model)
    
    window.RailsModel = RailsModel
    

    You can use it like

    model = new RailsModel
    model.url = "/data"
    model.fetch()
    model.get('excercise/duration')
    model.set_field('excercise/duration', 25)
    

    The last line will trigger an event “change:excercise/duration”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Each of my clients can have many todo items and every todo item has
How would one implement shuffle for the Celestial Jukebox? More precisely, at each time
assuming that you have the following sentence in a requirements document The system has
Assuming following definition: /// <summary> /// Replaces each occurrence of sPattern in sInput with
I would like to ask about Memory Overhead in java, I have a large
I have a search page that loads results. Each row has a Delete button
This is more of a general question as I have found relatively little information
I have a couple of applications that depend on each other. One is a
i have the following code: $.each(data.People, function (i, person) { html.push(<img title=' + person.Name
Should I be separating my js for each page assuming there is no overlap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.