简体   繁体   English

我该如何从mvc3模型的序列化json中获取ribs.js模型?

[英]How do I fetch a backbone.js model from serialized json from a mvc3 model?

If I had a model like this in my MVC3 application: 如果我在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; }
}

How would do I populate it with .fetch() from backbone.js? 我该如何在.fetch()使用.fetch()填充它?

This is what I tried: 这是我尝试的:

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( ... ) // ?

I have the appropriate JsonResult Action method and control, and have verified with Fiddler that the fetch() call is properly returning the Json data. 我有适当的JsonResult Action方法和控件,并已通过Fiddler验证fetch()调用正确返回了Json数据。 (Which I can post tomorrow morning from the office) (明天早上我可以在办公室发帖)

I'm really new at Backbone, what am I doing wrong? 我真的是Backbone的新手,我在做什么错?

Couple things: 1) fetch is async. 两件事:1)提取是异步的。 Try this: 尝试这个:

person.fetch({
    success: function() {
        alert(person.get('Name')
    }
});

2) This will show that the name property is an object (but not a backbone model): 2)这将表明name属性是一个对象(而不是主干模型):

{
    Name: "Joe"
    etc...

}

You may want to use Backbone.Relational or something similar if you want Name to be a backbone model. 如果希望Name作为骨干模型,则可能需要使用Backbone.Relational或类似的方法。 or you can override parse to flatten out your json. 或者您可以覆盖解析以使json扁平化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM