2015-11-12 41 views
0

在當前基於Asp.net WebForms的項目中,我們使用requirejs在不同的頁面上加載js,模塊。我只想檢查以下是否是設置連接到特定頁面功能的「模塊」的正確方法。requirejs「頁面模塊」的結構

對於需要更多「獨特」腳本的一些特定頁面,我們設置了我們所說的「頁面模塊」。因爲它並不需要公開自己的範圍內,我們不使用定義,而是外部方法只需指定這樣的:

require(['main'], function() { 

    require(['jquery', 'app/Ajax', 'underscore'], function ($, ajax, _) { 

     _.templateSettings = { 
      interpolate: /\{\{(.+?)\}\}/g, 
      evaluate: /\{%([\s\S]+?)%\}/g, 
      escape: /\{%-([\s\S]+?)%\}/g 
     }; 

     var pageModule = function() { 
      this.init(); 
     }; 

     pageModule.prototype.init = function() { 
      var self = this; 
      //init functionality 
     }; 

     pageModule.prototype.method = function() { 
      // function used by the pageModule itself 
     } 

     return new pageModule(); 
    }); 
}); 

然後在HTML頁面的底部,我們所說的需要

<script> 
    require(['pages/pageModule']); 
</script> 

試圖尋找像這樣的模式,以確認這是一種利用功能但沒有發現太多東西的好方法。也許這是因爲這不是使用它的好方法,或者它被稱爲特定的東西。大多數示例使用了定義,但由於該模塊不應該被任何其他函數使用,所以定義似乎是多餘的。

回答

0

下面是官方多頁的例子,看起來很像你有什麼:

https://github.com/requirejs/example-multipage

例如page1.js:

//Load common code that includes config, then load the app logic for this page. 
requirejs(['./common'], function (common) { 
    requirejs(['app/main1']); 
}); 

而且在page1.html:

<script data-main="js/page1" src="js/lib/require.js"></script> 

(注:requirejs指的是s ame函數爲require

+0

在他們的例子中,app/main2.js被定義爲AMD模塊(使用define),那麼推薦的方法還是我的結構定義pageModule仍然正確? – Hyzac