【问题标题】:backbone.js - handling model relationships in a RESTful wayBackbone.js - 以 RESTful 方式处理模型关系
【发布时间】:2011-11-04 03:01:57
【问题描述】:

我正在使用backbone.js

例如,假设我们有一个“产品”模型和一个“类别”模型,它们具有多对多的关系。在我的一个观点中,假设我需要检索所有类别的列表,并知道每个类别是否与当前产品模型相关。

我是否设置了一个“类别”集合并将其作为我的模型的属性,并以某种方式授予它访问模型 id 的权限,以便在获取它时只获取相关的类别?然后我可以获取所有类别并交叉检查它们,看看哪些是相关的,而哪些是不相关的?

我不知道最好的方法是什么。我习惯使用 ORM,这使得它在服务器端变得容易。

【问题讨论】:

    标签: javascript orm model backbone.js relationship


    【解决方案1】:

    【讨论】:

    • 我同上骨干关系。很遗憾它不是主干开发的一部分。
    【解决方案2】:

    假设您在后端使用连接表:

    创建一个包含连接表上所有行的集合和模型,并将以下方法添加到集合中:productsByCategorycategoriesByProduct em>(使用 [加入集合].where(...)。)

    让 Backbone 中的数据在后端镜像您的数据似乎有助于使事情变得简单,并且您在设置 URL 时不必做任何复杂的事情。

    【讨论】:

      【解决方案3】:

      有一个简单且可定制的解决方案,尽管它可能不如backbone-relational 强大。

      Backbone.ModelWithRelationship = Backbone.Model.extend({
        mappings: {},
      
        set: function(attributes, options) {
          _.each(this.mappings, function(constructor, key) {
            var RelationshipClass = stringToFunction(constructor);
            var model = new RelationshipClass();
      
            /* New relational model */
            if (!this.attributes[key]) {  
              this.attributes[key] = (model instanceof Backbone.Collection) ? model : null;
            }
      
            /* Update relational model */
            if (attributes[key] && !(attributes[key] instanceof Backbone.Model || attributes[key] instanceof Backbone.Collection)) {
              if (model instanceof Backbone.Model) {
                this.attributes[key] = model;
                this.attributes[key].set(attributes[key], options);           
              } else if (model instanceof Backbone.Collection) {
                this.attributes[key].reset(attributes[key], options);         
              }
      
              delete attributes[key];
            } 
          }, this);
      
          return Backbone.Model.prototype.set.call(this, attributes, options);
        }
      });
      

      您可以通过创建Backbone.ModelWithRelationship 的子类来声明映射。

      Models.Post = Backbone.ModelWithRelationship.extend({
        mappings: {
          'comments': 'Collection.CommentCollection',
          'user': 'Models.User'
        }
      });
      

      【讨论】:

        【解决方案4】:

        http://pathable.github.com/supermodel/ 太棒了。它可以让您执行以下操作:

        Post.has().many('comments', {
          collection: Comments,
          inverse: 'post'
        });
        
        Comment.has().one('post', {
          model: Post,
          inverse: 'comments'
        });
        
        var post = Post.create({
          id: 1,
          comments: [{id: 2}, {id: 3}, {id: 4}]
        });
        
        post.comments().length; // 3
        var comment = Comment.create({id: 5, post_id: 1});
        post.comments().length; // 4
        comment.post() === post; // true :D
        

        【讨论】:

        • 我发现超模比骨干关系更简单、更重要。它有一个巧妙的设计,可以将模型的每个实例存储在自己的集合中,作为保持新鲜的存储中心点。
        猜你喜欢
        • 2014-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        相关资源
        最近更新 更多