     -1
 
   addToRefs = (aInstance, ctx)->
-    try
-      [vAddedRoles, vDelRoles] = getDiffRoles aInstance, ctx
-    catch err
-      return Promise.reject(err)
+    [vAddedRoles, vDelRoles] = getDiffRoles aInstance[rolesFieldName], ctx.hookState.oldRoles
 
     vId = aInstance.id
 

    else # if ctx.data # Partial update of possibly multiple models
      delete vInstance[permsFieldName]

    return Promise.resolve() unless vInstance[rolesFieldName]?
    vInstance[rolesFieldName] = getValidRoles vInstance[rolesFieldName]
    calcPerms vInstance, ctx
    .then (aPerms)->
      vInstance[permsFieldName] = aPerms
      ctx.options = {} unless ctx.options
      ctx.options.updatePermsByRefs = 0

  Model.observe 'after save',(ctx)->
    return Promise.resolve() if ctx.options and ctx.options.skipPropertyFilter

    vInstance = ctx.instance || ctx.data
    return Promise.resolve() unless vInstance and vInstance[rolesFieldName]?
    addToRefs(vInstance, ctx)

      result = []
    result

  getDiffRoles = (aNewRoles, aOriginalRoles)->
    if aOriginalRoles and aOriginalRoles.length
      vAddedRoles = []
      vDelRoles = []
      for i in aNewRoles
        vAddedRoles.push i if aOriginalRoles.indexOf(i) is -1
      for i in aOriginalRoles
        vDelRoles.push i if aNewRoles.indexOf(i) is -1
    else
      vAddedRoles = aNewRoles

    [getValidRoles(vAddedRoles), getValidRoles(vDelRoles)]

  indexOfRef = (aRefs, aModel, aId)->
