English 中文(简体)
与JSON的后骨关系模型
原标题:Serializing/Deserializing nested Backbone Relational models to JSON

考虑到以下关系模式模式:

var Server = Backbone.RelationalModel.extend({
    relations: [{
        type: Backbone.HasMany,
        key:  databases ,
        relatedModel:  Database ,
        collectionType:  DatabaseCollection ,
        includeInJSON:  id 
    }],

    defaults: {
        databases: []
    },
});

var Database = Backbone.RelationalModel.extend({});
var DatabaseCollection = Backbone.Collection.extend({
    model: Database
});

这些物体:

new Database({
    id: 1,
    name:  DB1 
});

new Database({
    id: 2,
    name:  DB2 
});

var s1 = new Server({
    id: 3,
    name:  S1 ,
    databases: [1,2]
});

将这种模式系列化或成型, 以何种最容易/最推荐的方式来质疑JSON的架构?

{
    databases: [
        { id: 1, name:  DB1  }
        { id: 2, name:  DB2  }
    ],

    servers: [
        { id: 3, name:  S1 , databases: [1, 2] }                 
    ]
}

这样就可以将数据发送到 / 从服务器上读取 。

谢谢!

蒂姆·蒂姆

问题回答

我用你的例子制作了你描述的JSON 我刚刚创建的这个小提琴小提琴小提琴有小小的改变 http://jsfiddle.net/scottluskcis/LQbjE/" rel=“nofollow” >example 。

我做了这些修改,是因为调试器显示一些警告,并取得了你描述的结果。希望这有帮助。

  1. Moved the declaration of Database Model and DatabaseCollection to top before Server since Servers relatedModel and CollectionType point to those Models.
  2. For relatedModel and collectionType instead of using Strings used the reference to Database and DatabaseCollection
  3. Created a collection for Servers called ServerCollection
  4. Added a few more examples

以下是你最后使用的代码, 我刚刚创建了一个普通的 Backbone 模型, 将两个收藏合并为一个。 呼叫JSON后, JSON给了你一个向服务器传输的单个 JSON 对象 。

var Database = Backbone.RelationalModel.extend({});
var DatabaseCollection = Backbone.Collection.extend({
    model: Database
});

var Server = Backbone.RelationalModel.extend({
    relations: [{
        type: Backbone.HasMany,
        key:  databases ,
        relatedModel: Database,
        collectionType: DatabaseCollection, 
        includeInJSON:  id  
    }],

    defaults: {
        databases: []
    } 
});
var ServerCollection = Backbone.Collection.extend({
    model: Server 
});

var allDatabases = new DatabaseCollection(); 
allDatabases.add([
    new Database({ id: 1, name:  DB1  }),  
    new Database({ id: 2, name:  DB2  }),
    new Database({ id: 3, name:  DB3  }),
    new Database({ id: 4, name:  DB4  })
]);   

var allServers = new ServerCollection(); 
allServers.add([
    new Server({
        id: 30,
        name:  S1 , 
        databases: [
          allDatabases.get(1),
          allDatabases.get(2)
        ]
    }),
    new Server({
        id: 40,
        name:  S2 ,
        databases: [
          allDatabases.get(3),
          allDatabases.get(4)
        ]
    })
]);  

// combine into an object to transfer to server as one 
var serverObject = new Backbone.Model({
     servers : allServers,
     databases : allDatabases
});  
console.log(serverObject.toJSON()); 




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签