我对 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;
});