简体   繁体   English

Backbone.js嵌套对象结构

[英]Backbone.js nested object structure

I am working on a project where we have data models that look basically like this... 我正在开发一个项目,我们的数据模型看起来基本上就像这样......

var library = {
    location: 'Somewhere'
    books: [
    {
        name: 'Good Book',
        publisher: 'Great publisher'
        authors: [
        {
            name: 'Joe Schmoe',
            age: '65'
        },
        {
            name: 'Robert Smith',
            age: '47'
        }
    }  
}

I am trying to figure out what the best way of laying out the model structure. 我试图找出布局模型结构的最佳方法。

Something like... 就像是...

var LibraryModel = Backbone.Model.extend({});

var Book = Backbone.Model.extend({});
var Books = Backbone.Collection.extend({
    model: Book
});

var Author = Backbone.Model.extend({});
var Authors = Backbone.Collection.extend({
    model: Author
});

Is this the best way to tackle this? 这是解决这个问题的最佳方法吗? Has anyone else faced anything like this? 还有其他人遇到过这样的事吗?

Whether or not you need N to N, 1 to N or whatever relationships is largely a back-end concern, IMO. IMO,你是否需要N到N,1到N或任何关系,这在很大程度上都是一个后端问题。 Unless you dealing strictly with CRUD screens, Backbone models typically don't translate directly to a back-end model, though they often approximate a back-end model. 除非您严格处理CRUD屏幕,否则Backbone模型通常不直接转换为后端模型,尽管它们通常接近后端模型。

That being said, Backbone-relational is definitely a good option for handling situations like this. 话虽这么说,Backbone-relational绝对是处理这种情况的好选择。 It provides a very robust set of features for handling relational code and is worth looking in to. 它为处理关系代码提供了一组非常强大的功能,值得一试。

If you'd rather stay clear of a large plugin and it's associated configuration and requirements, though, there are some simple tricks that you can use in your models directly. 但是,如果您想要清除大型插件及其相关配置和要求,则可以直接在模型中使用一些简单的技巧。 Since you know your data structure up front, you can make large assumptions about the code you need to write instead of having to create a very flexible system like BB-Relational. 由于您事先了解了自己的数据结构,因此可以对需要编写的代码进行大量假设,而不必创建一个非常灵活的系统,如BB-Relational。

For example, getting a list of book into a library, using the above data structure, can be as simple as: 例如,使用上述数据结构将书籍列表添加到库中可以如下所示:


Book = Backbone.Model.extend({});
Books = Backbone.Collection.extend({
  model: Book
});

Library = Backbone.Model.extend({
  initialize: function(){
    this.parseBooks();
  },

  parseBooks: function(){
    var data = this.get("books");
    this.unset("books", {silent: true});
    this.books = new Books(data);
  }
});

I've used this code half a dozen times and it's worked out just fine for me every time. 我已经使用了这个代码六次,每次都对我很好。 You can extend this same pattern and set of assumptions in to each layer of your object model. 您可以将相同的模式和假设集扩展到对象模型的每个层。

You might also need to override the toJSON method on your model: 您可能还需要覆盖模型上的toJSON方法:


Library = Backbone.Model.extend({
  // ... code from above, here

  toJSON: function(){
    var json = Backbone.Model.prototype.toJSON.call(this);
    json.books = this.books.toJSON();
    return json;
  }
});

Depending on how far down this path you need to go, you will likely end up re-creating half of the functionality that BB-Relational already provides. 根据您需要走多远的路径,您可能最终会重新创建BB-Relational已经提供的一半功能。 It might be easier to use a plugin like that, with declarative relationship handling if you have a large enough structure. 使用像这样的插件可能更容易,如果你有足够大的结构,可以使用声明性关系处理。

IMO, it's worth understanding these basic patterns, knowing how to do this for yourself before you dig in to BB-Relational. IMO,值得了解这些基本模式,在深入了解BB-Relational之前知道如何为自己做这件事。 But if you're constrained for time, I'd start with BB-Relational or some other relational mapper (I think there are a few more out there, these days). 但是,如果你受时间的限制,我会从BB-Relational或其他一些关系映射器开始(我认为现在还有一些更多)。

i would not tackle this that way, because you have an N to N relationship, 1 book can have multiple authors and 1 author has most likely written more than 1 book. 我不会那样解决这个问题,因为你有一个N到N的关系,一本书可以有多个作者,一个作者最有可能写一本以上的书。

i would take a look at Backbone Relational plugin 我会看一下Backbone Relational插件

it's not default backbone, but it does the trick the best way i can think off. 它不是默认的主干,但它是我能想到的最佳方式。

however if you only need a small solution you can just have all your authors in a collection, and use a simple author ID array in your book model. 但是,如果您只需要一个小型解决方案,您可以将所有作者都放在一个集合中,并在您的图书模型中使用一个简单的作者ID数组。

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

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