javascript 如何使用 Backbone.js 从嵌套的 JSON 构建集合/模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8782619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:34:12  来源:igfitidea点击:

How to build a Collection/Model from nested JSON with Backbone.js

javascriptbackbone.jsbackbone-relational

提问by 3logy

I'm relativly new to Backbone.js

我对 Backbone.js 比较陌生

I have a JSONlike the picture shows ! I saw some Answers in relation with Backbone-relational, but still dont get the point!

我有一个如图所示的JSON!我看到了一些与 Backbone-relational 相关的答案,但仍然不明白这一点!

How can i convert this JSONto Backbone.js Collections/Models??

如何将此JSON转换为 Backbone.js 集合/模型?

I update with a code but it dont work like expected! i can't see an model when i do :

我用代码更新,但它不像预期的那样工作!当我这样做时,我看不到模型:

My Structure is :

我的结构是:

[0] : is a collection of models

[0] : 是模型的集合

[clefs] + ... + [Rest] : are collection of models

[谱号] + ... + [Rest] : 是模型的集合

(clefs) => [0] + ... + [9] : are Models(title contains a string, path too)

(谱号)=> [0] + ... + [9] :是模型(标题包含一个字符串,路径也是)

Thanks a lot!!

非常感谢!!

EDIT(10.01.12) :

编辑(10.01.12):

My Solution :

我的解决方案:

window.initModel = Backbone.Model.extend({
  defaults: {
    "title": "",
    "path": ""
  }
});
window.CustomCollection = Backbone.Collection.extend({
  model: initModel
});
window.Init = Backbone.Model.extend({
  url : function(){
    return  "/api/data.json"      
  },

  parse: function(response) {

    clefs = new CustomCollection();
    clefs.add(response.clefs);        
    this.set({clefs: clefs});

    .....

    rests = new CustomCollection();
    rests.add(response.rests);        
    this.set({rests: rests});
} 
});

thishelped me out too!

这也帮助了我!

Nested Array

嵌套数组

回答by Daryl Teo

I'm at work, so I cannot give you a fully coded answer, but the gist is, you can do the following in your top level models to achieve a nested model hierarchy:

我在工作,所以我不能给你一个完整的编码答案,但要点是,你可以在你的顶级模型中执行以下操作来实现嵌套模型层次结构:

var AmericasNextTopModel = Backbone.Models.extend({
    initialize: function(){

        this.set({
             clefs: new ClefCollection(this.get('clefs')),
             accidentals: new AccidentalCollection(this.get('accidentals')),
             notes: new NoteCollection(this.get('notes')),
             rests: new RestCollection(this.get('rests'))
        });
    }
});

I do not use backbone-relational so I cannot give you an answer regarding that.

我不使用骨干关系,所以我不能给你一个答案。

Are you making an online sheet music viewer/editor? :D Cool I'd love to see it when you're done.

您正在制作在线乐谱查看器/编辑器吗?:D 酷我很想在你完成后看到它。

回答by djlumley

The resetmethod (see 'reset') allows you to pass a JSON array to a collection. This is the equivalent of a PUT method, replacing the specified collection with the JSON hash.

reset方法(请参阅 'reset')允许您将 JSON 数组传递给集合。这相当于 PUT 方法,用 JSON 哈希替换指定的集合。

You can also use the addmethod to add to an existing collection, or pass the JSON hash into the constructor as you create a new collection.

您还可以使用该add方法添加到现有集合,或在创建新集合时将 JSON 哈希传递到构造函数中。

You'll have to do some basic cleaning up of your array to get it in an appropriate format, and then convert it to JSON

您必须对数组进行一些基本的清理以将其转换为适当的格式,然后将其转换为 JSON

回答by webninjataylor

I'm using PHP to grab a feed as JSON since it's on a different domain. I save those results to a JS variable, and then I just had success using this to get it into my Backbone collection...

我正在使用 PHP 以 JSON 形式获取提要,因为它位于不同的域中。我将这些结果保存到一个 JS 变量中,然后我成功地使用它将它放入我的 Backbone 集合中......

var feedCollection = new Backbone.Collection();
feedCollection.set(myFeedJSON.nestedObject.nestedArrayIWant);