我对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;
});
答案 0 :(得分:1)
您的导入顺序似乎有一个小错误:
'jquery', -> $,
'underscore', -> _
'backbone', -> Backbone
'models/Panel', -> Panel
'models/ace', -> AceView
'views/ace', -> AceModel
'views/panel -> PanelView
您已交换models/ace
和views/ace