2014-02-17 36 views
0

有什麼方法可以在文本文檔中執行requirejs定義?或者一個html文檔?在一個文件中定義多個html amd模塊

我有填充有頭部和單元模板文件用於在網格

<body> 
    <th data-fieldname="PatientPerson">Name <span data-bind="attr: { class: sortField() == 'PatientPerson' ? 'inline-block' : 'hide' }"></span></th> 
    <td><span class="glyphicon glyphicon-expand"></span><a data-bind="click: function() { $parent.loadReportSummary(PatientPerson.ID()) }"><span data-bind="text: PatientPerson.FullName"></span></a></td> 

    <th data-fieldname="StudyType">Study Type <span data-bind="attr: { class: sortField() == 'StudyType' ? 'inline-block' : 'hide' }"></span></th> 
    <td data-bind="text: StudyType"></td> 


    <th data-fieldname="ServiceDate">Service Date<span data-bind="attr: { class: sortField() == 'ServiceDate' ? 'inline-block' : 'hide' }"></span></th> 
    <td data-bind="text: ServiceDate"></td> 


    <th>Export Summary</th> 
    <td><a data-bind="click: function (data) { $parent.exportReportSummary(data, PatientPerson.ID, SummaryID, !StudyExported()) }">View</a></td> 


    <th>Print All Reports</th> 
    <td><a data-bind="click: function (data) { $parent.printAllReports('/PrintReports/Reports?summaryID=' + SummaryID) }">Print</a></td> 

etc....... 
</body> 

在另一個模塊我有,它確定這些列都在計算觀察到的敲除使用的陣列。我希望我可以使這些模塊中的每一個都成爲一個模塊,而不是使用jquery解析它們,但是我希望它們都在一個文件中。我爲requirejs使用了文本插件,但似乎沒有辦法將每個文件都聲明爲一個文件內的模塊,並且將每個文件拆分爲單獨的文件似乎很浪費。

可能像

<!--export name:"PatientPerson" --> 
    <th data-fieldname="PatientPerson">Name <span data-bind="attr: { class: sortField() == 'PatientPerson' ? 'inline-block' : 'hide' }"></span></th> 
    <td><span class="glyphicon glyphicon-expand"></span><a data-bind="click: function() { $parent.loadReportSummary(PatientPerson.ID()) }"><span data-bind="text: PatientPerson.FullName"></span></a></td> 
<!-- /export--> 

然後引用模塊一樣

require('filename').PatientPerson; 

回答

0

我創建了一個插件,您可以添加一個!定義要對一些文件的要求最終,它會根據OP的工作做出規範建議。它適用於requirejs的官方文本插件。

define(['text', 'module'], function (text, module) { 
    var exportRegExp = /<!--\s*?export[^>]*>([\s\S]*?)<!--\s*?\/export\s*?-->/g, 
    masterConfig = (module.config && module.config()) || {}, 
    buildMap={}; 
    text.export = function (content) { 
     var exports = null; 
     if (content) { 
      var matches = content.match(exportRegExp) || [], 
       match, _i, _len; 
      exports = matches.length ? {} : null; 
      for (_i = 0, _len = matches.length; _i < _len; _i++) { 
       match = matches[_i]; 
       var exportName = match.match(/(<!--\s*?export\s*?name\:")(.*?)\"\s*?-->/).slice(-1)[0]; 
       exports[exportName] = match.replace(/<!--\s*?export[^>]*>\n?/, '').replace(/<!--\s*?\/export\s*?-->/, ''); 
      } 
     } 
     return exports; 
    }; 
    var baseParseName = text.parseName; 
    text.parseName = function(name) { 
     var index, parseExport = false; 
     index = name.indexOf('!export'); 
     if (index !== -1) { 
      parseExport = true; 
      name.split('!export').join(''); 
     } 
     var out = baseParseName(name); 
     out["strip"] = { "export": parseExport, "strip": out["strip"] }; 
     return out; 
    }; 

    text.finishLoad = function (name, strip, content, onLoad) { 
     var exports = strip.export ? text.export(content,name) : content; 
     if (exports == null) content = strip.strip ? text.strip(content) : content; 
     else content = exports; 
     if (masterConfig.isBuild) { 
      buildMap[name] = content; 
     } 
     onLoad(content); 
    }; 
    text.write = function (pluginName, moduleName, write, config) { 
     if (buildMap.hasOwnProperty(moduleName)) { 
      var content = text.jsEscape(buildMap[moduleName]); 
      write.asModule(pluginName + "!" + moduleName, 
          "define(function() { return '" + 
           content + 
          "';});\n"); 
     } 
    }; 
}); 

下面的工作

require([text!somefile!define],function(){}) 

這也適用,但如果該文件中存在的任何出口卻忽視了!條命令。

require([text!somefile!strip!define],function(){}) 

,那麼你可以調用

require(['someModuleNameInDefineComment'],function(){}) 

我還沒有處理條帶與年初確定的標題。

Here is a Gist

相關問題