gpt4 book ai didi

requirejs - 有没有更简洁的方法在 RequireJS 中包含依赖项

转载 作者:行者123 更新时间:2023-12-02 22:19:12 27 4
gpt4 key购买 nike

假设我有一个如下所示的模块:

define(['jquery', 'actions', 'util', 'text!../templates/dialog.html!strip', 'text!../templates/requestRow.html!strip', 'text!../templates/respondForm.html!strip'], function($, actions, util, tDialog, tRequestRow, tRespondForm) {

此模块包含写入客户端 UI 的大部分代码。它还加载我编写的几个其他模块以及使用 text.js 插件的 3 个 HTML 模板。我想知道是否有更简洁的方法来做到这一点?随着应用程序的增长,我可能需要加载额外的模板或模块,而且我的定义语句似乎会变得有点难看。我是否应该像这样将模板路径添加到 main.js 中的 require.config 中:

require.config({
baseUrl: '/webrequests/resources/scripts/',
paths: {
'modernizr': '../js/vendor/modernizr-2.6.2-respond-1.1.0.min',
'bootstrap' : '../js/vendor/bootstrap.min',
'dialog' : 'text!../templates/dialog.html!strip',
'requestRow' : 'test!../templates/requestRow.html!strip',
'respondForm' : 'text!../templates/respondForm.html!strip'
}});

是否有某种方法可以加载目录中的所有模板,并且只有 1 个依赖项包含在定义语句中?

提前致谢。

最佳答案

您可以制作一个模块来加载您经常使用的模板。因此,将要加载的模板分组到一个模块中,这样您就可以加载此模板模块而不是单个模板:

// generalTemplates.js
define([
'text!../templates/dialog.html!strip',
'text!../templates/requestRow.html!strip',
'text!../templates/respondForm.html!strip'
], function (tDialog, tRequestRow, tRespondForm) {
return {
dialog: tDialog,
requestRow: tRequestRow,
respondForm: tRespondForm
};
});

因此,在您的模块中,您可以像任何其他模块一样简单地包含模板:

define([
'jquery',
'actions',
'util',
'generalTemplates'
], function($, actions, util, templates) {
var tDialog = templates.dialog,
tRequestRow = templates.requestRow,
tRespondForm = templates.respondForm;

/* You can do stuff with the templates here */
});

关于requirejs - 有没有更简洁的方法在 RequireJS 中包含依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15755090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com