Creating nested models with backboneJS + backbone-relational + requireJS

前端 未结 4 771
陌清茗
陌清茗 2020-12-14 12:44

I am new to BackboneJS and I am stuck with nested relations using Backbone-relational Model with RequireJS -I think I runned into circular issues. Any help will be highly ap

4条回答
  •  感情败类
    2020-12-14 13:03

    From a comment in backbone-relational.js v0.5.0 (line 375):
    // 'exports' should be the global object where 'relatedModel' can be found on if given as a string.

    If you require the special 'exports' value as a dependency in your define call, and then place your module onto the exports object before you return, then you can reference that module as a string or as a member of exports.

    in ModuleModel.js:

    define(['exports', 'use!backbone', 'use!backbone-relational'], function(exports, Backbone) {
      var ModuleModel = Backbone.RelationalModel.extend({
        relations: [
          {
            type: Backbone.HasMany,
            key: 'groups',
            relatedModel: 'ModuleModel',
            collectionType: 'ModuleCollection'
          }
        ]
      });
      exports.ModuleModel = ModuleModel;
      return ModuleModel;
    });
    

    and in ModuleCollection.js:

    define(['exports', 'use!backbone'], function(exports, Backbone) {
      var ModuleCollection = Backbone.RelationalModel.extend({
        url: '/api/v1/module/';
        model: exports.ModuleModel;
      });
      exports.ModuleCollection = ModuleCollection;
      return ModuleCollection;
    });
    

提交回复
热议问题