Skip to content

Create metaMap when creating class hierarchy instead of eachRelationship #100

@mtheoryx

Description

@mtheoryx
	/**
	 * Calls the callback for each relationship defined on the model.
	 *
	 * @method eachRelationship
	 * @for Model
	 * @param {Function} callback Function that takes `name` and `meta` parameters
	 * @param [binding] Object to use as `this`
	 * @static
	 */
	eachRelationship: function(callback, binding) {
		// Climb through class hierarchy looking for relationships
		// (###TODO: Might be nice to wire this up when creating the class hierarchy if possible.)
		var classProto = this.prototype;
		while (classProto && classProto.metaMap) {
			if (classProto.metaMap['_all']) {
				for (var j = 0, len = classProto.metaMap['_all'].length; j < len; ++j) {
					var name = classProto.metaMap['_all'][j];
					callback.call(binding, name, classProto.metaMap[name]);
				}
			}
			// Might be better to do this in initialization somewhere rather than on every eachRelationship call;
			// however, only costs one typeof and one string match each call
			if (typeof Object.getPrototypeOf !== 'function') {
				if (typeof 'test'.__proto__ === 'object') { // eslint-disable-line no-proto
					Object.getPrototypeOf = function(object) {
						return object.__proto__;  // eslint-disable-line no-proto
					};
				} else {
					Object.getPrototypeOf = function(object) {
						// May break if the constructor has been tampered with
						return object.constructor.prototype;
					};
				}
			}
			classProto = Object.getPrototypeOf(classProto);
		}
	},

Could be beneficial to avoid this on each, even though the performance hit is not too terrible

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions