-1
@model = new Client()

new_object = new Object()
new_object.site_id = ""
new_object.UserName = ""
etc..

@model.set(
  Header1: new_object,
  Header2: somethingelse
)

@model.save()

How can i access my deeply nested models/objects? Trying the below does not work.

this.get('obj1').get('childObject').propertyName

Update:

I saw this article earlier [backbone.js get and set nested object attribute, but it was not clear on how he is accessing the childObject.

{ Obj1 :{
   Obj2: {
    //List of properties
   }
  }
}

When i set this to my Model class, how am i going to fetch the properties of Obj2.

theJava
  • 14,620
  • 45
  • 131
  • 172
  • possible duplicate of [backbone.js get and set nested object attribute](http://stackoverflow.com/questions/6351271/backbone-js-get-and-set-nested-object-attribute) – jevakallio Feb 04 '13 at 18:02
  • There's also a github project to simplify this with less chaining. https://github.com/amccloud/backbone-dotattr – Tom Chandler Feb 05 '13 at 13:49

1 Answers1

1

It should work exactly as you describe above, it seems like you might be mixing properties and attributes.

Here's an example of using both methods for child models:

var m1 = new Backbone.Model;
var c1 = new Backbone.Model, c2 = new Backbone.Model, c3 = new Backbone.Model;
c3.level = "final";
c1.set({'stuff' : 'this', 'child' : c3});
c1.prop = "go";
c2.set({'stuff' : 'that', 'child' : c3});
c2.prop = "stop";
m1.set({'child1' : c1});
m1.set({'child2': c2});
console.log(m1.get('child1').get('stuff'));
console.log(m1.get('child1').prop);
console.log(m1.get('child1').get('child').level);
console.log(m1.get('child2').get('stuff'));
console.log(m1.get('child2').prop);
console.log(m1.get('child2').get('child').level);

This should produce console output:

this
go
final
that
stop
final
Bryan Clark
  • 2,542
  • 1
  • 15
  • 19