创建嵌套型号backboneJS +骨干关系+ requireJS [英] Creating nested models with backboneJS + backbone-relational + requireJS

查看:190
本文介绍了创建嵌套型号backboneJS +骨干关系+ requireJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来BackboneJS和我坚持使用骨干关系模型RequireJS - 我想我拼命地跑成圆形嵌套问题的关系。任何帮助将是非常美联社preciated!

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 appreciated!

我有以下的型号和系列:

I have the following model and collection:

 /* Module Model at models/module*/
define([
'jquery', 
'underscore', 
'backbone',
'backboneRelational',
], function($, _, Backbone) {

    var ModuleModel = Backbone.RelationalModel.extend({

        urlRoot: 'api/module',
        _radius: 50,
        relations: [{
            type: Backbone.HasMany,
            key: 'children',
            relatedModel: 'ModuleModel',
            collectionType: 'ModuleCollection',
            reverseRelation: {
                key: 'parent_id',
                includeInJSON: 'id' 
            }
        }],
        url: function() {
            return this.id? 'api/module/' + this.id : 'api/module';
        }
    });
    return ModuleModel;
});

/* Module Collection */
define([
'jquery',
'underscore', 
'backbone', 
'models/module'
], function($, _, Backbone, ModuleModel) {

    var ModuleCollection = Backbone.Collection.extend({

        model: ModuleModel,
        url: 'api/modules'
    });

    return ModuleCollection;
});

当我初始化对象ModuleModel,它引发以下错误:

When I initialize the object ModuleModel, it throws the following error:

Relation=child; no model, key or relatedModel (function (){ parent.apply(this, arguments); }, "children", undefined)

我真的坚持了下来......你能为我指向正确的方向?非常感谢你提前!

I am really stuck with it... Could you point me to the right direction? Thank you very much in advance!

推荐答案

这看起来像一个作用域的问题。在 ModuleModel 初始化它要创建本身就是一个的hasMany 的关系,但它不能发现自己,它会给你说的错误形式悲痛:

This looks like a scoping issue. During initialization of ModuleModel it wants to create a hasMany relation with itself, but it can't find itself and it will give you grief in form of said error:

http://jsfiddle.net/yNLbq

一旦对象是在当前范围可达事情开始工作了:

Once the object is reachable from the current scope things start to work out:

http://jsfiddle.net/jDw5e

一个可能的解决方案是给模型,并收集自己命名空间可以从当前范围内到达。

A possible solution would be to give models and collection a namespace for themselves which can be reached from the current scope.

希望这有助于。

这篇关于创建嵌套型号backboneJS +骨干关系+ requireJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆