目前,我在数组中有一些视图路径,然后需要它们,但我想自动扫描文件夹的目录和子目录:
paths = ['a/', 'b/', 'c/', 'd/d2/', '']
for path in paths
try
View = require(path+name)
catch e
lastError = e
if View
return View
else
throw lastError
我尝试了以下操作,但似乎我无法使用File类:
paths = []
clientPath = __dirname.replace('/base', '') # go up a directory
currentFiles = []
currentFiles = fs.readdirSync(clientPath)
for file in currentFiles
checkFile = new File(clientPath + "/" + file).isDirectory()
if checkFile
paths.push(file + "/")
在Backbone中操作路径字符串的好方法是什么?
答案 0 :(得分:0)
如果此代码在浏览器中运行,那你就不走运了。出于安全原因,JavaScript在浏览器doesn't have access to the filesystem中运行,因此您不能以这种方式要求您的文件。
好消息是Require.js includes an optimiser that will compile your requirements for you。您还可以使用Grunt和Require.js plugin之类的内容来完成此构建步骤。