2014-08-29 62 views
0

我似乎無法從我的主應用程序文件訪問我的視圖。我是新來的骨幹和牽線木偶,我讀過這些文件,但無法找到爲什麼它不起作用。任何幫助或建議?Backbone.js&Marionette.js - 無法從主app.js文件訪問視圖

app.js

window.App = new Marionette.Application(); 

    App.addRegions({ 
     gameCriteria: "#game-criteria" 
    }); 

    App.myRegion.show(myView); 

}); 

myView.js

App.module("Views", function(Views, App, Backbone, Marionette, $, _) { 

    var GameCriteriaTab = Backbone.Marionette.ItemView.extend({ 
     template: JST["game-criteria-tab"], 

     regions: { 
      rulesRegion: "#rules-region" 
     }, 

     onShow: function() { 
      this.rulesRegion.show(RulesView); 
     } 
    }); 

}); 

回答

0

你instatiate調用前戲您的看法?我正在等待類似的代碼:

App.myRegion.show(new GameCriteriaTab ({ 
              model: new GameCriteriaModel() 
             })); 

你可以提供我們在鉻控制檯中的錯誤?

+0

它只是說我的觀點沒有定義 – conor909 2014-08-29 15:32:27

+0

是的,那是因爲你沒有定義它 – 2014-08-29 18:21:45

0

不知道你在這裏試圖做什麼。我假設你要創建一個名爲'windowApp'的應用程序,並且你正試圖在名爲'gameCriteria'的區域內顯示一個名爲'GameCriteriaTab'的視圖。我重構你的代碼如下: -

windowApp = new Backbone.Marionette.Application(); 

windowApp.addRegions({ 
    gameCriteria: "#game-criteria" 
}); 

windowApp.GameCriteriaTab = Marionette.ItemView.extend({ //Defining the view 
    template: " <include your template Id or className here> " 
});  

windowApp.on("start",function(){ 
    var myView = new windowApp.GameCriteriaTab(); //You need to create an instance of the view if you want to render it in the page 
    windowApp.gameCriteria.show(myView); //Showing the view in the specified region 
}); 

windowApp.start(); //This will run the marionette application 

不能定義ItemView控件內的任何區域。你想創建一個嵌套視圖嗎?在這種情況下,LayoutView將滿足您的需求,並且您可以在其中添加區域。只需將ItemView更改爲LayoutView。

+0

是的,這就是我正在做的。但是,如果ItemView是在一個單獨的文件中呢?在index.html頭標記中的app.js後面調用。此外,window.App被調用,因爲我試圖讓我所有的文件JS在DOM中可用。我的應用程序不叫「windowApp」 – conor909 2014-08-29 15:55:35

+0

在這種情況下,以這種方式構建您的代碼: - 在myView.js中查看定義,這一行代碼 - 在另一個名爲appStart.js的文件中的「windowApp.start()」,以及app.js中的其餘代碼然後,您可以按照以下順序將這些文件包含在index.html中: - app.js,myView.js和appStart.js – booleanhunter 2014-08-29 15:58:35

+0

另外,如果我將ItemView更改爲LayoutView,則會出現以下錯誤:無法讀取undefined屬性'extend' – conor909 2014-08-29 15:59:20