2012-07-13 125 views
5

我建立一個web應用程序使用CodeIgniter在前面的背部和骨幹/ RequireJS使用RequireJS並在不同的頁面骨幹。因此,它不是一個單一的網頁應用程序,而是仍然使用CI來執行很多控制器操作,並且在不同的時間使用Backbone來處理各種視圖。我有RequireJS /骨幹網的設置是這樣的:的笨web應用程序

<script type="text/javascript" data-main="main" src="require.js"></script> 

main.js樣子:

require.config({ 
    paths: { 
      jquery: "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min", 
      underscore: "libs/underscore", 
      backbone: "libs/backbone", 
      //Lots of other stuff 
      }, 
    shim: { 
      backbone: { 
       deps: ["underscore", "jquery"], 
      } 
      // lots of other stuff 
    } 
    }); 

require(['views/app'], function(AppView){ 
    var app_view = new AppView; 
}); 

Main.js加載在每一頁上我的主模板,即,反過來加載了一堆意見認爲在整個網站/應用是常見的 - 頭,資產淨值等

但在不同的網頁我也想,除了常見的加載不同的看法。本身幾乎是一個應用程序 - - 因此,例如,一個名爲頁上的「發佈」我做的:

<script type="text/javascript" src="publish.js"></script> 

這是main.js加載後,看起來像:

//publish.js 
require(['views/publishview], function(PublishView){ 
var publishView = new PublishView; 
}); 

基本上,它工作一半的時間,另一半(當我錘子刷新)我得到404s的一切。

那麼,有沒有用我的基本需要的一切配置一個整潔的方式,但也需要在不同的時間不同的部分?如果整個事情都是SPA,似乎很容易,但對我來說,應用的很大一部分仍然是CodeIgniter。

對不起,不措辭這更好。

回答

1

不知道這是正確的方式,但我會做什麼:

模板(PHP)

<script type="text/javascript"> 
var myApp = {}; 
myApp.section = '<?= $section ?>'; //for exambple 'someSection' 
</script> 
<script type="text/javascript" data-main="main" src="require.js"></script> 

然後,使用這樣的:

(main.js)

var requires = { 
someSection: {files: ['views/app'], handler: function(AppView){ 
    var app_view = new AppView; 
}} 
}; 

var myRequire = requires[myApp.section] || null; 
if(myRequire) require(myRequire.files, myRequire.handler); 

或...使用window.location.pathname值代替myApp.section