亞當正確的上下文中,但他的代碼片斷是錯誤的。
,將工作的函數是這樣的:
<cffunction name="$createNestedParamStruct" returntype="struct" access="public" output="false">
<cfargument name="params" type="struct" required="true" />
<cfscript>
var loc = {};
for(loc.key in arguments.params)
{
if (Find("[", loc.key) && Right(loc.key, 1) == "]")
{
// object form field
loc.name = SpanExcluding(loc.key, "[");
// we split the key into an array so the developer can have unlimited levels of params passed in
loc.nested = ListToArray(ReplaceList(loc.key, loc.name & "[,]", ""), "[", true);
if (!StructKeyExists(arguments.params, loc.name))
arguments.params[loc.name] = {};
loc.struct = arguments.params[loc.name]; // we need a reference to the struct so we can nest other structs if needed
loc.iEnd = ArrayLen(loc.nested);
for(loc.i = 1; loc.i lte loc.iEnd; loc.i++) // looping over the array allows for infinite nesting
{
loc.item = loc.nested[loc.i];
if (!StructKeyExists(loc.struct, loc.item))
loc.struct[loc.item] = {};
if (loc.i != loc.iEnd)
loc.struct = loc.struct[loc.item]; // pass the new reference (structs pass a reference instead of a copy) to the next iteration
else
loc.struct[loc.item] = arguments.params[loc.key];
}
// delete the original key so it doesn't show up in the params
StructDelete(arguments.params, loc.key, false);
}
}
</cfscript>
<cfreturn arguments.params />
</cffunction>
我在我的應用進行了測試(CFWheels外)它完美地工作。你所做的只是傳入一個結構體(在我的例子中是來自FW/1的Rc結構體),它包含什麼應該是結構體,但是顯示爲字符串,並且你將返回一個嵌套結構體。
例子:
<cfscript>
Struct['hello[world]'] = 1;
Struct['hello[earth]'] = 2;
myhello = $createNestedParamStruct(Struct);
/* Now myhello equals this:
myhello.hello.world = 1;
myhello.hello.eath = 2;
*/
</cfscript>
FWIW,你的表格範圍已經是一個結構。所有變量範圍都是如此。 – ale 2011-03-01 16:27:14
@Al是的所有變量範圍都有自己的結構,但在FW/1中,我的表單和url範圍與其他一些東西混合在一起。就我所知,我只需要我的表單範圍,但無法訪問。 – 2011-03-01 17:19:23
正如Al所說,form是一個可以像其他任何結構一樣操作的結構。 – 2011-03-01 17:19:43