主干关系 FetchRelated 不获取任何内容

发布于 2024-12-22 03:29:28 字数 1925 浏览 1 评论 0原文

尝试整合一个后台解决方案来清理混乱的 JavaScript,最终会成为一项痛苦的工作。我肯定做错了什么,但找不到任何解决方案。 该数据包含许多具有嵌套模型 CarsType 的车库,这些车库具有以下属性(名称、描述)。 每种汽车类型在每个车库中使用一次,但可以在许多车库中找到。 每个车库的汽车类型是任意排序的 所以关系表 car_type/garage name pack 是这样定义的 |编号 |车库_id |汽车类型 ID |位置|

每个汽车类型都分配给许多机械师。每个机械师都可以在许多车库工作。 所以第三个嵌套层是关系表mechanic_pack |mechanic_id|pack_id|

后端是 Rails 3.1,可以输出 Json。

Garage = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasMany, 
            key: 'Packs',
            relatedModel: 'pack',
            collectionType: 'Packs',
            reverseRelation: {
              key: 'garage',
                includeInJSON: 'id' 
            }
        }
    ],

    initialize: function(attributes) {
        this.fetchRelated("packs");
      },

    url : function() {
      var base = 'garages';
      if (this.isNew()) return base;
      return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
    }
///... more method ...

});

packs 集合在另一个文件模型中定义,

Packs= Backbone.Collection.extend({
    model: Pack, 
    url: '/packs/list/:garage_id'
});

可以正确加载,但 fetchRelated 不会获取并且不会引发任何错误。 Firebugs 中的断点显示 toFetch 始终未定义

fetchRelated: function( key, options ) {
  options || ( options = {} );
  var rel = this.getRelation( key ),
  keyContents = rel && rel.keyContents,
  toFetch = keyContents && _.select( _.isArray( keyContents ) ? keyContents : [ keyContents ], function( item ) {
  var id = _.isString( item ) || _.isNumber( item ) ? item : item[      rel.relatedModel.prototype.idAttribute ];
  return id && !Backbone.Relational.store.find( rel.relatedModel, id );
  }, this );
  if ( toFetch && toFetch.length ) { 
......

我应该如何调用 fetchRelated?

第二个问题: 当请求车库时,rails 会发送完整的嵌套模型树。有没有办法从初始 json 答案填充 backones 嵌套模型?并能够用自己的视图来操纵每个嵌套模型?

Trying to put together a backone solution to clean messy javascript, ends up to be a painful endeavour. Surely I'm doing something wrong, but cannot find any solution.
the data contains many garages that have nested model CarsType with following attributes(name,description).
Each carType are used once per garage but can be found in many garages.
carType are sorted arbitrary per garage
so the relation table car_type/garage name pack is defined this way
| id | garage_id | car_type_id | position |

Each carType are assign to many mechanics. each mechanic can work in many garages.
so the third nested level is a relation table mechanic_pack
|mechanic_id|pack_id|

Backend is rails 3.1 that spits out Json.

Garage = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasMany, 
            key: 'Packs',
            relatedModel: 'pack',
            collectionType: 'Packs',
            reverseRelation: {
              key: 'garage',
                includeInJSON: 'id' 
            }
        }
    ],

    initialize: function(attributes) {
        this.fetchRelated("packs");
      },

    url : function() {
      var base = 'garages';
      if (this.isNew()) return base;
      return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
    }
///... more method ...

});

packs collections is defined in another file

Packs= Backbone.Collection.extend({
    model: Pack, 
    url: '/packs/list/:garage_id'
});

model gets loaded correctly but fetchRelated doesn't fetch and doesn't throw any errors. Breakpoint in Firebugs shows that toFetch is always undefined

fetchRelated: function( key, options ) {
  options || ( options = {} );
  var rel = this.getRelation( key ),
  keyContents = rel && rel.keyContents,
  toFetch = keyContents && _.select( _.isArray( keyContents ) ? keyContents : [ keyContents ], function( item ) {
  var id = _.isString( item ) || _.isNumber( item ) ? item : item[      rel.relatedModel.prototype.idAttribute ];
  return id && !Backbone.Relational.store.find( rel.relatedModel, id );
  }, this );
  if ( toFetch && toFetch.length ) { 
......

How should I invoke fetchRelated?

2nd question:
When a garage is requested rails sends the full tree of nested models. is there a way to populate backones nested models from the initial json answer? and be able to manipulate each nested model with their own view?

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

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

发布评论

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

评论(1

你列表最软的妹 2024-12-29 03:29:28
  initialize: function(attributes) {
    this.fetchRelated("Packs");
  }

另外, relatedModel 'pack' 和与您的集合关联的模型之间似乎存在不一致: Pack

如果它仍然不起作用,您是否尝试为您的密钥指定一个与您的 collectionType 不同的名称?

  initialize: function(attributes) {
    this.fetchRelated("Packs");
  }

Also, there seems to be an inconsistency between the relatedModel 'pack' and the model associated with your collection : Pack

If it still does not work, have you tried giving your key a name that is not the same as your collectionType ?

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