如何使用 Backbone.js 从嵌套的 JSON 构建集合/模型 [英] How to build a Collection/Model from nested JSON with Backbone.js

查看:21
本文介绍了如何使用 Backbone.js 从嵌套的 JSON 构建集合/模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Backbone.js 比较陌生

I'm relativly new to Backbone.js

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

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

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

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

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

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

我的结构是:

[0] : 是模型的集合

[0] : is a collection of models

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

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

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

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

非常感谢!!

编辑(10.01.12):

我的解决方案:

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});
} 
});

这个也帮了我一把!

推荐答案

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

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.

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

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

这篇关于如何使用 Backbone.js 从嵌套的 JSON 构建集合/模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆