0

我对 Backbone 有点陌生,在下面的代码中,当我使用 RequireJSAceModel加载时,我无法调用构造函数。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;
});
4

1 回答 1

1

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

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

您已交换models/aceviews/ace

于 2013-03-05T07:15:54.883 回答