       @_validateFields document.Meta.fields
 
   @defineAll: (dontThrowDelayedErrors) ->
-    globals.Document._retryDelayed not dontThrowDelayedErrors
+    loopMax =>
+      globals.Document._retryDelayed not dontThrowDelayedErrors
     globals.Document.validateAll()
 
     assert dontThrowDelayedErrors or globals.Document._delayed.length is 0

    @documents = new @_Manager @Meta

    @_addDelayed @
    @_retryDelayed()

  @list = []
  @_delayed = []


  collection

loopMax = (f) ->
  for i in [0..MAX_RETRIES]
    throw "Possible infinite loop" if i is MAX_RETRIES
    break unless f()

class globals.Document
  # TODO: When we will require all fields to be specified and have validator support to validate new objects, we can also run validation here and check all data, reference fields and others
  @objectify: (parent, ancestorArray, obj, fields) ->
