简体   繁体   中英

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. 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.

That being said, Backbone-relational is definitely a good option for handling situations like this. 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.

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:


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. 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. 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).

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.

i would take a look at Backbone Relational plugin

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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