2013-03-05 112 views
0

我對Backbone有點新穎,在下面的代碼中,當我使用RequireJS加載AceView時,我無法調用AceModel的構造函數。也就是說,如果我在snap.js中刪除AceView參數傳遞迴調函數,我可以調用new AceModel(),它可以工作,但如果我有AceView,那麼構造函數將失敗,並顯示錯誤:TypeError: Cannot call method 'get' of undefinedRequireJS + 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; 
}); 

回答

1

看來你有一個小錯誤的順序您的進口:

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

已經兌換models/aceviews/ace

+0

哇。我沒有意識到他們需要有序。這讓我難以忍受今天最長的一段時間(facepalm)。謝謝! – user1027169 2013-03-05 07:18:23