diff --git a/lib/Associations/Extend.js b/lib/Associations/Extend.js index a6d42ac1..2e63ab30 100644 --- a/lib/Associations/Extend.js +++ b/lib/Associations/Extend.js @@ -214,7 +214,11 @@ function autoFetchInstance(Instance, association, opts, cb) { opts.autoFetchLimit = association.autoFetchLimit; } - if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch)) { + // autoFetchLimit has been reached - + // opts.autoFetch is present, so it overrides association.autoFetch + // opts.autoFetch not present, use value from original assn definition + if (opts.autoFetchLimit === 0 || (typeof opts.autoFetch !== "undefined" && ! opts.autoFetch) + || (typeof opts.autoFetch === "undefined" && !association.autoFetch)) { return cb(); } diff --git a/lib/Associations/Many.js b/lib/Associations/Many.js index 1500772d..2a0e2d9b 100644 --- a/lib/Associations/Many.js +++ b/lib/Associations/Many.js @@ -475,7 +475,11 @@ function autoFetchInstance(Instance, association, opts, cb) { opts.autoFetchLimit = association.autoFetchLimit; } - if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch)) { + // autoFetchLimit has been reached - + // opts.autoFetch is present, so it overrides association.autoFetch + // opts.autoFetch not present, use value from original assn definition + if (opts.autoFetchLimit === 0 || (typeof opts.autoFetch !== "undefined" && ! opts.autoFetch) + || (typeof opts.autoFetch === "undefined" && !association.autoFetch)) { return cb(); } diff --git a/lib/Associations/One.js b/lib/Associations/One.js index df8a3a46..fbf1ebcd 100644 --- a/lib/Associations/One.js +++ b/lib/Associations/One.js @@ -297,7 +297,11 @@ function autoFetchInstance(Instance, association, opts, cb) { opts.autoFetchLimit = association.autoFetchLimit; } - if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch)) { + // autoFetchLimit has been reached - + // opts.autoFetch is present, so it overrides association.autoFetch + // opts.autoFetch not present, use value from original assn definition + if (opts.autoFetchLimit === 0 || (typeof opts.autoFetch !== "undefined" && ! opts.autoFetch) + || (typeof opts.autoFetch === "undefined" && !association.autoFetch)) { return cb(); } diff --git a/lib/Model.js b/lib/Model.js index c684c4d2..8dcac8d2 100644 --- a/lib/Model.js +++ b/lib/Model.js @@ -81,7 +81,7 @@ function Model(opts) { } var assoc_opts = { - autoFetch : inst_opts.autoFetch || false, + autoFetch : inst_opts.autoFetch, autoFetchLimit : inst_opts.autoFetchLimit, cascadeRemove : inst_opts.cascadeRemove }; @@ -92,6 +92,21 @@ function Model(opts) { ExtendAssociation.extend(model, instance, opts.driver, extend_associations, assoc_opts); }; + // check all the auto-fetch associations have been populated for the given instance + var hasAutoFetchAssociationsLoaded = function(instance) { + var checkAssn = function(associations) { + for (var assn = 0; assn < associations.length; assn++) { + if(associations[assn].autoFetch && ! instance.hasOwnProperty(associations[assn].name)) { + return false; + } + }; + return true; + }; + return checkAssn(one_associations) && + checkAssn(many_associations) && + checkAssn(extend_associations); + }; + var pending = 2, create_err = null; var instance = new Instance(model, { uid : inst_opts.uid, // singleton unique id @@ -112,6 +127,7 @@ function Model(opts) { extend_associations : extend_associations, association_properties : association_properties, setupAssociations : setupAssociations, + hasAutoFetchAssociationsLoaded: hasAutoFetchAssociationsLoaded, fieldToPropertyMap : fieldToPropertyMap, keyProperties : keyProperties }); @@ -275,9 +291,6 @@ function Model(opts) { conditions[prop.mapsTo] = ids[i]; } - if (!options.hasOwnProperty("autoFetch")) { - options.autoFetch = opts.autoFetch; - } if (!options.hasOwnProperty("autoFetchLimit")) { options.autoFetchLimit = opts.autoFetchLimit; } @@ -299,7 +312,8 @@ function Model(opts) { Singleton.get(uid, { cache : (options.hasOwnProperty("cache") ? options.cache : opts.cache), - save_check : opts.settings.get("instance.cacheSaveCheck") + save_check : opts.settings.get("instance.cacheSaveCheck"), + autoFetch : options.autoFetch }, function (cb) { return createInstance(data[0], { uid : uid, @@ -406,15 +420,23 @@ function Model(opts) { uid += "/" + data[opts.keys[i]]; } - // Now we can do the cache lookup + // When there is extra_info we need a slightly longer uid for the Singleton lookup + if(merge && options.extra_info) { + for(var j=0; j