Last active
August 25, 2015 21:57
-
-
Save chrisdpeters/4acd3ae3a6e0208e4ee5 to your computer and use it in GitHub Desktop.
CFWheels nestedProperties sortProperty fix
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
<cfcomponent extends="Model" output="false"> | |
<cffunction name="init" output="false"> | |
<cfscript> | |
hasMany(name="attemptedSolutions", shortcut="solution", dependent="delete"); | |
nestedProperties(association="attemptedSolutions", allowDelete=true, sortProperty="sortOrder"); | |
beforeValidation("$adjustAttemptedSolutionSortOrders"); | |
</cfscript> | |
</cffunction> | |
<cffunction name="$adjustAttemptedSolutionSortOrders" access="private" hint="Adjusts array of attempted solutions before save happens so there aren't any null entries." output="false"> | |
<cfscript> | |
if (StructKeyExists(this, "attemptedSolutions") && ArrayLen(this.attemptedSolutions)) { | |
local.attemptedSolutions = []; | |
local.arrayPos = 1; | |
for (local.i = 1; local.i <= ArrayLen(this.attemptedSolutions); local.i++) { | |
// Exception will be thrown when array element is "undefined" or null | |
try { | |
local.attemptedSolution = this.attemptedSolutions[local.i]; | |
local.attemptedSolution.sortOrder = local.arrayPos; | |
ArrayAppend(local.attemptedSolutions, local.attemptedSolution); | |
local.arrayPos++; | |
} | |
catch (any e) {} | |
} | |
this.attemptedSolutions = local.attemptedSolutions; | |
} | |
</cfscript> | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment