RequireJS + Backbone:导入View模块与Model模块有某种冲突

时间:2013-03-05 06:53:43

标签: javascript backbone.js requirejs

我对Backbone有点新,在下面的代码中,当我使用RequireJS加载AceModel时,我无法调用AceView的构造函数。也就是说,如果我在AceView中删除回调函数中的snap.js参数传递,我可以调用new AceModel()并且它可以工作,但如果我有AceView那么构造函数会失败错误:TypeError: Cannot call method 'get' of undefined

这里发生了什么?

查看/ snap.js

define([
    'jquery',
    'underscore',
    'backbone',
    'models/Panel',
    'models/ace',
    'views/ace',
    'views/panel'
], function ($, _, Backbone, Panel, AceView, AceModel, PanelView) {
    'use strict';

    var SnapView = Backbone.View.extend({

        initialize: function () {
            this.el = '#snap';
            this.$el = $(this.el);
            this.$elLoadSbml = this.$el.children().find('div#loadSbml');
            this.loadSbmlView = new AceView({
                el: this.$elLoadSbml[0],
                model: new AceModel({mode: 'monokai'})
            });
        },

    });

    return SnapView;
});

视图/ ace.js

define([
    'jquery',
    'underscore',
    'backbone'
],
    function($, _, Backbone) {
        'use strict';

        var AceView = Backbone.View.extend({
            initialize: function () {
                this.editor = ace.edit(this.el);
                this.editor.setTheme("ace/theme/" + this.model.get('theme'));
                this.editor.getSession().setMode("ace/mode/" + this.model.get('mode'));
                this.render();
            },

            render: function () {
                $(this.el).height(this.model.get('height'));
                $(this.el).width(this.model.get('width'));
                this.editor.resize();
            }
        });
        return AceView;
    });

模型/ ace.js

define([
    'jquery',
    'underscore',
    'backbone'
], function ($, _, Backbone) {
    'use strict';

    var AceModel = Backbone.Model.extend({
        defaults: {
            height: 800,
            width: 400,
            theme: 'github',
            mode: 'xml'
        }
    });

    return AceModel;
});

1 个答案:

答案 0 :(得分:1)

您的导入顺序似乎有一个小错误:

'jquery', -> $,
'underscore', -> _
'backbone', -> Backbone
'models/Panel', -> Panel
'models/ace', -> AceView
'views/ace', -> AceModel
'views/panel -> PanelView

您已交换models/aceviews/ace