Backbone.js 嵌套对象结构

发布于 2025-01-03 06:35:47 字数 730 浏览 0 评论 0原文

我正在开发一个项目,我们的数据模型基本上看起来像这样......

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

我试图找出布置模型结构的最佳方式。

就像......

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

这是解决这个问题的最佳方法吗?还有其他人遇到过这样的事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的绿洲 2025-01-10 06:35:47

在我看来,无论你是否需要 N 对 N、1 对 N 或任何关系,很大程度上都是后端问题。除非您严格处理 CRUD 屏幕,否则 Backbone 模型通常不会直接转换为后端模型,尽管它们通常近似于后端模型。

话虽这么说,骨干关系绝对是处理此类情况的一个不错的选择。它提供了一组非常强大的功能来处理关系代码,值得研究。

不过,如果您不想使用大型插件及其相关的配置和要求,那么您可以直接在模型中使用一些简单的技巧。由于您预先了解数据结构,因此您可以对需要编写的代码做出大量假设,而不必创建像 BB-Relational 这样非常灵活的系统。

例如,使用上述数据结构将书籍列表放入图书馆可以非常简单:


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

我已经使用此代码六次了,每次都对我来说效果很好。您可以将相同的模式和假设集扩展到对象模型的每一层。

您可能还需要重写模型上的 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;
  }
});

根据您需要沿着这条路径走多远,您可能最终会重新创建 BB-Relational 已提供的一半功能。如果您有足够大的结构,那么使用这样的插件可能会更容易,并且具有声明性关系处理。

IMO,在深入研究 BB-Relational 之前,了解这些基本模式、了解如何为自己做到这一点是值得的。但如果您时间有限,我会从 BB-Relational 或其他一些关系映射器开始(我认为现在还有更多)。

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

忆依然 2025-01-10 06:35:47

我不会那样处理这个问题,因为你们有 N 对 N 的关系,
一本书可以有多个作者
并且 1 位作者很可能写了超过 1 本书。

我会看一下 Backbone Relational 插件,

它不是默认的主干,但它的效果最好我可以思考的方式。

但是如果您只需要一个小解决方案
您可以将所有作者放在一个集合中,并在图书模型中使用简单的作者 ID 数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文