Created
October 3, 2012 23:06
-
-
Save jfrux/3830458 to your computer and use it in GitHub Desktop.
Railo Component THIS Scope Quirks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
work = new my_worker(); | |
work.usesUtil(); | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component name="my_worker" { | |
public any function init() { | |
variables.utils = new utilities(); | |
return this; | |
} | |
remote any function usesUtil() { | |
var myArray = ["test1","test2","test3"]; | |
return utils.mapper(myArray,this.mapAction); | |
} | |
public any function beforeUsesUtil() { | |
var myInfo = getComponentMetaData(this); | |
writeOutput("Before usesUtil() I still have my THIS scope!<br />I am '#myInfo.name#'.<hr />"); | |
} | |
public any function mapAction() { | |
var myInfo = getComponentMetaData(this); | |
//I need my this so I can call my own functions but I don't have them anymore! | |
try { | |
this.duringUsesUtil(); | |
} catch (any err) { | |
writeOutput("Called during usesUtil()... oops, no scope :-\"); | |
writeDump(var=err.message); | |
} | |
writeOutput("Called from usesUtil() I've lost my identity!<br />I am '#myInfo.name#'.<hr />"); | |
} | |
public any function duringUsesUtil() { | |
var myInfo = getComponentMetaData(this); | |
} | |
public any function afterUsesUtil() { | |
var myInfo = getComponentMetaData(this); | |
writeOutput("After usesUtil() I no longer have my THIS scope<br />I am '#myInfo.name#'.<hr />"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Before usesUtil() I still have my THIS scope!<br />I am 'test.my_worker'.<hr />Called during usesUtil()... oops, no scope :-\ | |
<!-- ==start== dump {ts '2012-10-03 19:45:24'} format: html --> | |
<div id="x30n"><script language="JavaScript" type="text/javascript"> | |
function dumpOC(name){var tds=document.all?document.getElementsByTagName('tr'):document.getElementsByName(name);var s=null;name=name;for(var i=0;i<tds.length;i++) {if(document.all && tds[i].name!=name)continue;s=tds[i].style;if(s.display=='none') s.display='';else s.display='none';}} | |
</script> | |
<style type="text/css"> | |
div#x30n table {font-family:Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:11px; empty-cells:show; color:#000;} | |
div#x30n td {border:1px solid #000; vertical-align:top; padding:2px; empty-cells:show;} | |
div#x30n td span {font-weight:bold;} | |
td.rfc9 {background-color:#fc9;} | |
td.rf60 {background-color:#f60;} | |
</style> | |
<table cellspacing="1"><tr><td class="rf60" title="C:\Projects\semver\test\my_worker.cfc:26" >string</td><td class="rfc9" title="C:\Projects\semver\test\my_worker.cfc:26" >component [utilities] has no function with name [duringUsesUtil]</td></tr></table></div> | |
<!-- ==stop== dump --> | |
Called from usesUtil() I've lost my identity!<br />I am 'test.utilities'.<hr />Called during usesUtil()... oops, no scope :-\ | |
<!-- ==start== dump {ts '2012-10-03 19:45:24'} format: html --> | |
<div id="x30p"><script language="JavaScript" type="text/javascript"> | |
function dumpOC(name){var tds=document.all?document.getElementsByTagName('tr'):document.getElementsByName(name);var s=null;name=name;for(var i=0;i<tds.length;i++) {if(document.all && tds[i].name!=name)continue;s=tds[i].style;if(s.display=='none') s.display='';else s.display='none';}} | |
</script> | |
<style type="text/css"> | |
div#x30p table {font-family:Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:11px; empty-cells:show; color:#000;} | |
div#x30p td {border:1px solid #000; vertical-align:top; padding:2px; empty-cells:show;} | |
div#x30p td span {font-weight:bold;} | |
td.rfc9 {background-color:#fc9;} | |
td.rf60 {background-color:#f60;} | |
</style> | |
<table cellspacing="1"><tr><td class="rf60" title="C:\Projects\semver\test\my_worker.cfc:26" >string</td><td class="rfc9" title="C:\Projects\semver\test\my_worker.cfc:26" >component [utilities] has no function with name [duringUsesUtil]</td></tr></table></div> | |
<!-- ==stop== dump --> | |
Called from usesUtil() I've lost my identity!<br />I am 'test.utilities'.<hr />Called during usesUtil()... oops, no scope :-\ | |
<!-- ==start== dump {ts '2012-10-03 19:45:24'} format: html --> | |
<div id="x30r"><script language="JavaScript" type="text/javascript"> | |
function dumpOC(name){var tds=document.all?document.getElementsByTagName('tr'):document.getElementsByName(name);var s=null;name=name;for(var i=0;i<tds.length;i++) {if(document.all && tds[i].name!=name)continue;s=tds[i].style;if(s.display=='none') s.display='';else s.display='none';}} | |
</script> | |
<style type="text/css"> | |
div#x30r table {font-family:Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:11px; empty-cells:show; color:#000;} | |
div#x30r td {border:1px solid #000; vertical-align:top; padding:2px; empty-cells:show;} | |
div#x30r td span {font-weight:bold;} | |
td.rfc9 {background-color:#fc9;} | |
td.rf60 {background-color:#f60;} | |
</style> | |
<table cellspacing="1"><tr><td class="rf60" title="C:\Projects\semver\test\my_worker.cfc:26" >string</td><td class="rfc9" title="C:\Projects\semver\test\my_worker.cfc:26" >component [utilities] has no function with name [duringUsesUtil]</td></tr></table></div> | |
<!-- ==stop== dump --> | |
Called from usesUtil() I've lost my identity!<br />I am 'test.utilities'.<hr />After usesUtil() I no longer have my THIS scope<br />I am 'test.my_worker'.<hr /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component name="utilities" { | |
public any function init() { | |
return this; | |
} | |
public array function mapper(obj, iterator) { | |
var result = []; | |
if (isArray(arguments.obj)) { | |
var index = 1; | |
var resultIndex = 1; | |
for (element in arguments.obj) { | |
if (!arrayIsDefined(arguments.obj, index)) { | |
index++; | |
continue; | |
} | |
var local = {}; | |
local.tmp = iterator(element, index, arguments.obj); | |
if (structKeyExists(local, "tmp")) { | |
result[resultIndex] = local.tmp; | |
} | |
index++; | |
resultIndex++; | |
} | |
} | |
else if (isObject(arguments.obj) || isStruct(arguments.obj)) { | |
var index = 1; | |
for (key in arguments.obj) { | |
var val = arguments.obj[key]; | |
var local = {}; | |
local.tmp = iterator(val, key, arguments.obj); | |
if (structKeyExists(local, "tmp")) { | |
result[index] = local.tmp; | |
} | |
index++; | |
} | |
} | |
else { | |
// query or something else? convert to array and recurse | |
result = _.map(toArray(arguments.obj), iterator); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment