2014-02-17 4 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에 대한 텍스트 플러그인을 사용하고 있지만 각 모듈을 하나의 파일 안에 모듈로 선언 할 수있는 방법이 없을 것입니다. 이러한 파일을 각각 별도의 파일로 분할해야하는 것은 낭비입니다.

어쩌면 당신이 추가 할 수 있습니다 그럼 난 플러그인을 만들어

require('filename').PatientPerson; 

답변

0

같은 모듈을 참조

<!--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--> 

같은! 다음은 몇 가지 파일이 필요합니다 끝으로 정의하고, 그것은 것입니다 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

관련 문제