How to build deeply nested models in Backbone.js v0.5

Assuming that each A has 0 or more B, and each B has 0 or more elements C. How to represent 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 url variables in the following 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 
+6
source share
1 answer

I would not create a sub-URL unless you really have a good argument. Each resource can be identified by a resource name and identifier. The connection between objects is internal.

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

This means that many server calls to pull out nested objects are not really ideal. You better have a single resource that returns nested json

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

For example, the data returned 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" } } 

Under the nodes are collected by the user controller, which takes individual db records and creates a data tree.

However, you have problems right now because backbone.js initially only supports flat structures. I made some changes to the underlying Backbone.Model to support a processing tree similar to structures.

I'll put it here if it can come in handy.

 #= 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 as

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

The last line will raise the event "change: excercise / duration"

+7
source

Source: https://habr.com/ru/post/892016/


All Articles