Skip to content

Instantly share code, notes, and snippets.

@datapimp
Created December 12, 2012 08:49
Show Gist options
  • Select an option

  • Save datapimp/4266192 to your computer and use it in GitHub Desktop.

Select an option

Save datapimp/4266192 to your computer and use it in GitHub Desktop.
# Takes an string like "deep.nested.value" and an object like window
# and returns the value of window.deep.nested.value. useful for defining
# references on objects which don't yet exist, as strings, which get
# evaluated at runtime when such references will be available
Luca.util.resolve = (propertyReference, source_object)->
return propertyReference unless _.isString(propertyReference)
try
source_object ||= (window || global)
resolved = _( propertyReference.split(/\./) ).inject (obj,key)->
obj = obj?[key]
, source_object
catch e
console.log "Error resolving", propertyReference, source_object
throw e
resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment