2012-10-03 2 views
0

모든 내 deps가 발견되어로드되었지만 mapApp.js 파일 인 내 응용 프로그램은 발견되지 않으며 사용하려고 할 때 항상 내게 Undefined를 제공합니다.JS가 내 응용 프로그램을 찾을 수 없음을 요구합니다.

내가 뭘 잘못하고 있니?

이 계층 적 내 폴더

Site 
    | 
    |- JS 
     |- Libs 
     | |- * All my deps * 
     | 
     |- mapApp.JS 
     | 
     . 
     . 
     . 
     |- /models 
     |- /views 
     |- /collections 

이 require.js

를 초기화 내 Main.js 파일입니다
require.config({ 
    baseUrl: '/ctt-ct/js/' 
    ,urlArgs: "ts=" + (new Date()).getTime() 

    ,paths: { 
     'jquery': 'libs/jquery.min' 
     ,'underscore': 'libs/underscore-min' 
     ,'backbone': 'libs/backbone' 
     ,'templates': '../templates' 
    } 

    ,shim: { 
    jquery: { 
     exports: '$' 
    } 
    ,underscore: { 
     exports: '_' 
    } 
    ,backbone: { 
     deps: ['underscore', 'jquery'], 
     exports: 'Backbone' 
    } 
    } 
}); 

require([ 
    'jquery' 
    ,'underscore' 
    ,'backbone' 
    ,'mapApp' 
], 
function ($, _, Backbone, App) { 
    $;      // <- working 
    _;      // <- working 
    Backbone.View;   // <- working 
    var app = new App();  // <- NOT working !!! 
}); 

mapApp.js

require([ 
    'jquery' 
    ,'underscore' 
    ,'backbone' 
], 
function ($, _, Backbone) { 
    var App = Backbone.View.extend({ 

     el : $('#map_canvas')  
     ,initialize : function(){ 
       // DO a lot of stuff and don't return anything. 
     } 

     ,drawData: function(){ 
       // Do other stuff. 
     } 
    }); 
}); 

답변

1

당신은에서 응용 프로그램을 반환해야 함수 :

... 
function ($, _, Backbone) { 
    var App = Backbone.View.extend({ 

    }); 

    return App; 
}); 

일반적으로 사용하지 않아도되지만, 올바른 방법이 확실하지 않습니다 (문서가 매우 친숙하지 않음). 나는 종종 작성합니다

mapApp.js

define([ 
    'views/otherView' //Other BackboneView 
], 
function (OtherView) { 
    var App = Backbone.View.extend({ 

     el : $('#map_canvas')  
     ,initialize : function(){ 
      // stuff ; not too much in a View 
     } 

     ,render : function(){ 
      var otherView = new OtherView(); 
      ... 
      return this; 
     } 
    }); 
    return App; 
}); 

를하는 경우, 백본, 밑줄 및 jQuery를 페이지에서 전역 변수입니다. 나는 당신이 항상 그들을 필요로하기 때문에 그것이 합리적이라고 생각합니다.

+0

당신은 그것을 찍었습니다! 내 백본 객체를 모든 클래스에 저장하는 var을 반환해야합니까? 같은 모델과 컬렉션도? 나는 그다지 좋아하지 않는다. ( –

+0

필요하다. IntelliJ를 위해 150 $ 라이센스를 구입했을 때, IDE에 꽤 많은 고통이있다. 나는 당신이 더 혼란스러워 할 필요가 없다고 생각한다. 이 모든 변수. –

관련 문제