我该如何从mvc3模型的序列化json中获取bone.js模型? [英] How do I fetch a backbone.js model from serialized json from a mvc3 model?
问题描述
如果我在MVC3应用程序中有这样的模型:
If I had a model like this in my MVC3 application:
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 ()
来自lobb.js?
How would do I populate it with .fetch()
from backbone.js?
这是我尝试的方法:
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数据。 (我明天早上可以在办公室张贴帖子)
I have the appropriate JsonResult Action method and control, and have verified with Fiddler that the fetch()
call is properly returning the Json data. (Which I can post tomorrow morning from the office)
我真的是Backbone的新人,我在做什么错了?
I'm really new at Backbone, what am I doing wrong?
推荐答案
夫妇:1)提取是异步的。试试这个:
Couple things: 1) fetch is async. Try this:
person.fetch({
success: function() {
alert(person.get('Name')
}
});
2)这将显示name属性是一个对象(但不是骨干模型):
2) This will show that the name property is an object (but not a backbone model):
{
Name: "Joe"
etc...
}
您可能想要使用 Backbone的名字与您的关系相似。 。或者您可以覆盖解析来展平json。
You may want to use Backbone.Relational or something similar if you want Name to be a backbone model. or you can override parse to flatten out your json.
这篇关于我该如何从mvc3模型的序列化json中获取bone.js模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!