[英]How do I fetch a backbone.js model from serialized json from a mvc3 model?
如果我在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; }
}
我该如何在.fetch()
使用.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的新手,我在做什么错?
两件事:1)提取是异步的。 尝试这个:
person.fetch({
success: function() {
alert(person.get('Name')
}
});
2)这将表明name属性是一个对象(而不是主干模型):
{
Name: "Joe"
etc...
}
如果希望Name作为骨干模型,则可能需要使用Backbone.Relational或类似的方法。 或者您可以覆盖解析以使json扁平化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.