Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code>http://foo.com/a/:id.json http://foo.com/b/:id.json http://foo.com/c/:id.json </code></pre> <p>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</p> <pre><code>http://foo.com/a/:id.json </code></pre> <p>For example the data I get back from my server looks like</p> <pre><code>{ "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" } } </code></pre> <p>The sub nodes are assembled by a custom controller that takes the individual db records and makes a tree of data.</p> <p>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.</p> <p>I'll paste it here if it might be of use to you.</p> <pre><code>#= require jquery #= require underscore #= require backbone # class RailsModel extends Backbone.Model initialize: (attributes) -&gt; data = {} for k,v of @attributes if v.constructor.name == "Object" data[k] = new RailsModel(v) @set data, {silent: true} get: (field)-&gt; 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)-&gt; 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 </code></pre> <p>You can use it like</p> <pre><code>model = new RailsModel model.url = "/data" model.fetch() model.get('excercise/duration') model.set_field('excercise/duration', 25) </code></pre> <p>The last line will trigger an event "change:excercise/duration"</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload