Skip to content

Instantly share code, notes, and snippets.

@ryanswrt
Created April 22, 2015 13:04
Show Gist options
  • Save ryanswrt/67860b160aa06308dbd9 to your computer and use it in GitHub Desktop.
Save ryanswrt/67860b160aa06308dbd9 to your computer and use it in GitHub Desktop.
Denormalized Relation - infinite loop bug?
Utils.getDescendantProp = (obj, desc)->
arr = desc.split(".")
first = arr[0]
while arr.length and obj[first]
obj = obj[first]
first = arr.shift()
return obj
Schemas.buildDenormalizedRelation = (collection,fields,pk,fk)->
pk = pk ? '_id'
fk = fk ? '_id'
schema = {}
selectWidget = 'select2'
schema[fk] = {
type: String
regEx: SimpleSchema.RegEx.Id
autoform:
type: selectWidget
options: ->
collection.find({}).fetch().map((doc)->
label: fields.map((field)->"#{Utils.getDescendantProp(doc,field)} ").reduce(Reduce.sum)
value: doc[pk]
)
}
fields.forEach((field)->
schema[field] = {
#TODO: Figure out how to get correct datatype from remote collection schema
type: Utils.getDescendantProp(collection.simpleSchema()?._schema ? {},field)?.type ? String
autoform:
omit: true
autoValue: ->
doc_id = this.siblingField(fk).value
if doc_id
refDoc = collection.findOne({_id: doc_id})
return Utils.getDescendantProp(refDoc,field)
}
)
new SimpleSchema schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment