0
我对Backbone有点新颖,在下面的代码中,当我使用RequireJS加载AceView
时,我无法调用AceModel
的构造函数。也就是说,如果我在snap.js
中删除AceView
参数传递回调函数,我可以调用new AceModel()
,它可以工作,但如果我有AceView
,那么构造函数将失败,并显示错误:TypeError: Cannot call method 'get' of undefined
。RequireJS + Backbone:导入视图模块与模型模块冲突
这是怎么回事?
视图/ 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;
});
哇。我没有意识到他们需要有序。这让我难以忍受今天最长的一段时间(facepalm)。谢谢! – user1027169 2013-03-05 07:18:23