0

次のセットアップがあります(疑似コーヒーコード)。モデルとコレクションは、Require.js を使用して読み込まれます。

ParentModel = Backbone.Model.extend

ParentCollection = Backbone.Collection.extend

CollectionA = ParentCollection.extend
    model: ModelA

CollectionB = ParentCollection.extend
    model: ModelB

CollectionC = ParentCollection.extend
    model: ModelC

ModelA = ParentModel.extend
    defaults:
        collectionB: new CollectionB()
        collectionC: new CollectionC()

ModelB = ParentModel.extend
    defaults:
        collectionA: new CollectionA()

ModelC = ParentModel.extend
    defaults:
        collectionA: new CollectionA()

ModelA には、「子」モデルを持つ 2 つのコレクションがあります。ModelB と ModelC にはその逆があります。「親」モデルを持つ 1 つのコレクションです。ModelA は正常に動作しますが、ModelB と ModelC は 2 つのエラーを生成します。Firebug の spy.js による 1 つ目: 「モジュール名 'modelB' はコンテキスト用にまだロードされていません: _」と Require.js による 2 つ目: 「モジュール名 'collectionB' はコンテキスト用にまだロードされていません: _」. モデル B と C でコレクションを読み込まないと、エラーは発生せず、アプリは動作します。エラーを解決しようとしていますが、何が問題なのかわかりません。Backbone.js の循環参照の問題ですか、それとも Require.js の循環依存関係ですか?

編集

組織のコード.coffee (モデルA)

define (require) ->
    _ = require 'underscore'
    mGroup = require 'models/object/group/group'
    cDepartement = require 'collections/object/group/departement'
    cProject = require 'collections/object/group/project'

    mGroup.extend
        'urlRoot': '/api/organisation'
        'defaults': _.extend({}, mGroup.prototype.defaults,
            'type': 'organisation'
            'departements': new cDepartement()
            'projects': new cProject())

project.coffee のコード (モデル B)

define (require) ->
    _ = require 'underscore'
    mGroup = require 'models/object/group/group'
    cOrganisation = require 'collections/object/group/organisation'

    mGroup.extend
        'urlRoot': '/api/project'
        'defaults': _.extend({}, mGroup.prototype.defaults,
             'type': 'project'
             'organisations': new cOrganisation())

cOrganisation = require... と new cOrganisation をコメントアウトすると、すべてが機能します。プロジェクト、部門、および組織はすべてグループですが、組織はプロジェクトおよび部門の親です。

4

1 に答える 1

1

はい。デフォルトをinitializeメソッドに移動するだけです。すべての定義がロードされるときに使用されます。何かのようなもの:

ModelB = ParentModel.extend
  initialize(options) ->
    options = options || {}
    if options.collectionA
      this.collectionA = options.collectionA
    else
      this.collectionA = new CollectionA()
于 2012-07-26T08:52:33.330 に答える