问题描述:

@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.

网友答案:

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
相关阅读:
Top