| 51 | }; |
| 52 | |
| 53 | var createInstance = function (data, inst_opts, cb) { |
| 54 | if (!inst_opts) { |
| 55 | inst_opts = {}; |
| 56 | } |
| 57 | |
| 58 | var found_assoc = false, i, k; |
| 59 | |
| 60 | for (k in data) { |
| 61 | if (k === "extra_field") continue; |
| 62 | if (opts.properties.hasOwnProperty(k)) continue; |
| 63 | if (inst_opts.extra && inst_opts.extra.hasOwnProperty(k)) continue; |
| 64 | if (opts.keys.indexOf(k) >= 0) continue; |
| 65 | if (association_properties.indexOf(k) >= 0) continue; |
| 66 | |
| 67 | for (i = 0; i < one_associations.length; i++) { |
| 68 | if (one_associations[i].name === k) { |
| 69 | found_assoc = true; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | if (!found_assoc) { |
| 74 | for (i = 0; i < many_associations.length; i++) { |
| 75 | if (many_associations[i].name === k) { |
| 76 | found_assoc = true; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | if (!found_assoc) { |
| 82 | delete data[k]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | var assoc_opts = { |
| 87 | autoFetch : inst_opts.autoFetch || false, |
| 88 | autoFetchLimit : inst_opts.autoFetchLimit, |
| 89 | cascadeRemove : inst_opts.cascadeRemove |
| 90 | }; |
| 91 | |
| 92 | var setupAssociations = function (instance) { |
| 93 | OneAssociation.extend(model, instance, opts.driver, one_associations, assoc_opts); |
| 94 | ManyAssociation.extend(model, instance, opts.driver, many_associations, assoc_opts, createInstance); |
| 95 | ExtendAssociation.extend(model, instance, opts.driver, extend_associations, assoc_opts); |
| 96 | }; |
| 97 | |
| 98 | var pending = 2, create_err = null; |
| 99 | var instance = new Instance(model, { |
| 100 | uid : inst_opts.uid, // singleton unique id |
| 101 | keys : opts.keys, |
| 102 | is_new : inst_opts.is_new || false, |
| 103 | isShell : inst_opts.isShell || false, |
| 104 | data : data, |
| 105 | autoSave : inst_opts.autoSave || false, |
| 106 | extra : inst_opts.extra, |
| 107 | extra_info : inst_opts.extra_info, |
| 108 | driver : opts.driver, |
| 109 | table : opts.table, |
| 110 | hooks : opts.hooks, |