如果我的MVC3应用程序中有这样的模型:
public class Person
{
public Guid Id { get; set; }
public Name Name { get; set; }
public Address Address { get; set; }
public PhoneNumber PhoneNumber { get; set; }
}
public class Name
{
public string First { get; set; }
public string Last { get; set; }
}
public class Address
{
public string AddressLine { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
我如何使用来自backbone.js的.fetch()
填充它?
这就是我的尝试:
class Person extends Backbone.Model
$ ->
person = new Person()
person.fetch()
// person.get for things like Name.First, or Name, or First
// all return undefined
alert person.get( ... ) // ?
我有适当的JsonResult Action方法和控件,并已与Fiddler验证fetch()
调用正确返回Json数据。 (我明天早上可以从办公室发帖)
我是Backbone的新手,我做错了什么?
答案 0 :(得分:3)
偶然的事情:1)fetch是异步的。试试这个:
person.fetch({
success: function() {
alert(person.get('Name')
}
});
2)这将显示name属性是一个对象(但不是骨干模型):
{
Name: "Joe"
etc...
}
如果您希望Name成为骨干模型,您可能需要使用Backbone.Relational或类似的东西。或者你可以覆盖parse来压扁你的json。