从 CFC 返回结果之前对 AJAX 查询执行字符串函数
我的页面顶部“AZ”上有字母。当用户单击某个字母时,我会对 CFC 执行 AJAX 调用,CFC 根据所选字母执行缩略图目录查询并返回相应文件名的列表。
当列表返回到页面上的函数时,我使用 split() 对文件名执行多个函数以提取某些信息。例如,文件名的格式如下: lastname_firstnameIMG_1234_RGB.jpg
我的目标是提取 CFC 中的姓氏、名字和号码,以便我可以标记缩略图并创建指向的链接用于下载的“真实”文件,例如 lastname_firstnameIMG_1234_CMYK.tif
等...
最初,我构建了 AJAX 函数来使用 split()
处理返回的列表,但我'我在 IE 中遇到错误。为了缓解这个问题,我宁愿在 CFC 中执行字符串函数并返回准备就绪的数据。
在返回数据结构之前,如何在 CFC 中执行字符串函数?
编辑 此设置在 Safari 和 FF 中运行良好,但在 IE 中产生错误。我尝试修改 split()
函数并取得了一些成功,但是,当然,它在 FF 中无法正常工作。同样,我的目标是消除 javascript 字符串 mods 并在 CFC 中完成。
这是我的 CFC:
<cffunction name="List" access="remote" output="no" returntype="struct">
<cfargument name="letter" required="yes" type="string">
<cfset local = StructNew()>
<cfset local.response = StructNew()>
<cfset local.response["error"] = "">
<cfset local.response["message"] = "">
<cfset local.data = #arguments.letter# & "*.jpg">
<cfdirectory
action="list"
directory="#thumbsdir#"
recurse="true"
listinfo="name"
name="qFile"
filter="#local.data#"
/>
<cfset local.response["message"] = #qFile#>
<cfreturn local.response>
</cffunction>
和我的 AJAX 函数(包含在 document.ready 函数中...):
$('.lastname').click(function(e){
e.preventDefault();
$('#emptymessage').hide();
$('#searching').show();
$('#content').html("");
var alpha = $(this).attr('id');
$.getJSON("cfcengine.cfc?method=List&returnformat=json&queryFormat=column",
{letter:alpha},
function(res, code) {
var s = "";
if(res.message.ROWCOUNT > 0) {
$('#searching').hide();
for(var i=0; i<res.message.ROWCOUNT; i++) {
//This is all the stuff I want to get rid of...
var theFile = res.message.DATA.Name[i]
var theLastName = theFile.split(/_(.+)/)[0];
var theRest = theFile.split(/_(.+)/)[1];
var theFirstNameAll = theRest.split(/_(.+)/)[0];
var theFirstName = theFirstNameAll.split(/(?:IMG)/)[0];
var theImageAll = theRest.split(/_(.+)/)[1];
var theImage = theImageAll.split(/_(.+)/)[0];
var bw = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_BW.jpg';
var rgb = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_RGB.jpg';
//Right now I'm just returning the name to the page until debugging is complete...
s += '<p>' + res.message.DATA.Name[i] + '<\/p>';
}
} else {
var s = "Sorry, nothing matched your search.";
}
$("#content").html(s);
//End response
}
//End getJSON
);
//End plist click function
});
My page has the alphabet across the top 'A-Z'. When a user clicks on a letter I perform an AJAX call to a CFC that performs a query of a directory of thumbnail images based on the letter selected and returns a list of the corresponding file names.
When the list is returned to my function on the page I'm using split()
to perform several functions on the file name to pull out certain information. For example, the file names are formatted thusly: lastname_firstnameIMG_1234_RGB.jpg
My goal is to extract the last name, the first name and the number in my CFC so that I can label the thumbnail image and create links to the 'real' files for downloading such as lastname_firstnameIMG_1234_CMYK.tif
etc...
Originally, I built my AJAX function to work with the returned list using split()
but I'm getting errors in IE. To mitigate the problem I would rather perform the string functions in my CFC and return the data ready to go.
How do I perform the string functions in the CFC before returning the data struct?
EDIT This set-up worked famously in Safari and FF but produced an error in IE. I tried modifying the split()
functions and had some success but then, of course, it didn't work properly in FF. Again, my goal is to eliminate the javascript string mods and do it in the CFC.
Here's my CFC:
<cffunction name="List" access="remote" output="no" returntype="struct">
<cfargument name="letter" required="yes" type="string">
<cfset local = StructNew()>
<cfset local.response = StructNew()>
<cfset local.response["error"] = "">
<cfset local.response["message"] = "">
<cfset local.data = #arguments.letter# & "*.jpg">
<cfdirectory
action="list"
directory="#thumbsdir#"
recurse="true"
listinfo="name"
name="qFile"
filter="#local.data#"
/>
<cfset local.response["message"] = #qFile#>
<cfreturn local.response>
</cffunction>
And my AJAX function (which is wrapped in a document.ready function...):
$('.lastname').click(function(e){
e.preventDefault();
$('#emptymessage').hide();
$('#searching').show();
$('#content').html("");
var alpha = $(this).attr('id');
$.getJSON("cfcengine.cfc?method=List&returnformat=json&queryFormat=column",
{letter:alpha},
function(res, code) {
var s = "";
if(res.message.ROWCOUNT > 0) {
$('#searching').hide();
for(var i=0; i<res.message.ROWCOUNT; i++) {
//This is all the stuff I want to get rid of...
var theFile = res.message.DATA.Name[i]
var theLastName = theFile.split(/_(.+)/)[0];
var theRest = theFile.split(/_(.+)/)[1];
var theFirstNameAll = theRest.split(/_(.+)/)[0];
var theFirstName = theFirstNameAll.split(/(?:IMG)/)[0];
var theImageAll = theRest.split(/_(.+)/)[1];
var theImage = theImageAll.split(/_(.+)/)[0];
var bw = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_BW.jpg';
var rgb = theLastName + '_' + theFirstName + 'IMG_' + theImage + '_RGB.jpg';
//Right now I'm just returning the name to the page until debugging is complete...
s += '<p>' + res.message.DATA.Name[i] + '<\/p>';
}
} else {
var s = "Sorry, nothing matched your search.";
}
$("#content").html(s);
//End response
}
//End getJSON
);
//End plist click function
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 JavaScript,我会取消正则表达式文字并简化
split()
:以下是我在
...
中执行此操作的方法代码>:For JavaScript, I would do away with the regex literals and simplify the
split()
:Here's how I'd do it in
<cfscript>...</cfscript>
:ColdFusion 部分可以使用一行代码来完成,
因为 _ 在您的情况下实际上充当分隔符。
ListFirst 和 ListLast 是非常方便的函数。
The ColdFusion piece of this could be done in a single line of code using
because the _ really acts as a delimiter in your case.
ListFirst and ListLast are very handy functions.