Prechádzať zdrojové kódy

move parsablemodelmixin to own file

Adam Shaw 8 rokov pred
rodič
commit
e73f72d68a

+ 1 - 0
src.json

@@ -8,6 +8,7 @@
     "common/Class.js",
     "common/Class.js",
     "common/EmitterMixin.js",
     "common/EmitterMixin.js",
     "common/ListenerMixin.js",
     "common/ListenerMixin.js",
+    "common/ParsableModelMixin.js",
     "common/Model.js",
     "common/Model.js",
     "common/Promise.js",
     "common/Promise.js",
     "common/TaskQueue.js",
     "common/TaskQueue.js",

+ 85 - 0
src/common/ParsableModelMixin.js

@@ -0,0 +1,85 @@
+
+var ParsableModelMixin = {
+
+	standardPropMap: {}, // will be cloned by allowRawProps
+
+
+	/*
+	Returns true/false for success
+	*/
+	applyRawProps: function(rawProps) {
+		var standardPropMap = this.standardPropMap;
+		var manualProps = {};
+		var otherProps = {};
+		var propName;
+
+		for (propName in rawProps) {
+			if (standardPropMap[propName] === true) { // copy automatically
+				this[propName] = rawProps[propName];
+			}
+			else if (standardPropMap[propName] === false) {
+				manualProps[propName] = rawProps[propName];
+			}
+			else {
+				otherProps[propName] = rawProps[propName];
+			}
+		}
+
+		this.applyOtherRawProps(otherProps);
+
+		return this.applyManualRawProps(manualProps);
+	},
+
+
+	/*
+	If subclasses override, they must call this supermethod and return the boolean response.
+	*/
+	applyManualRawProps: function(rawProps) {
+		this.id = EventSource.normalizeId(rawProps.id);
+
+		if ($.isArray(rawProps.className)) {
+			this.className = rawProps.className;
+		}
+		if (typeof rawProps.className === 'string') {
+			this.className = rawProps.className.split(/\s+/);
+		}
+
+		return true;
+	},
+
+
+	applyOtherRawProps: function(rawProps) {
+		// subclasses can implement
+	}
+
+};
+
+
+/*
+TODO: devise a better system
+*/
+var ParsableModelMixin_allowRawProps = function(propDefs) {
+	var proto = this.prototype;
+
+	proto.standardPropMap = Object.create(proto.standardPropMap);
+
+	copyOwnProps(propDefs, proto.standardPropMap);
+};
+
+
+/*
+TODO: devise a better system
+*/
+var ParsableModelMixin_copyVerbatimStandardProps = function(src, dest) {
+	var map = this.prototype.standardPropMap;
+	var propName;
+
+	for (propName in map) {
+		if (
+			src[propName] != null && // in the src object?
+			map[propName] === true // false means "copy verbatim"
+		) {
+			dest[propName] = src[propName];
+		}
+	}
+};

+ 5 - 62
src/models/event-source/EventSource.js

@@ -1,5 +1,5 @@
 
 
-var EventSource = Class.extend({
+var EventSource = Class.extend(ParsableModelMixin, {
 
 
 	calendar: null,
 	calendar: null,
 
 
@@ -63,63 +63,15 @@ var EventSource = Class.extend({
 		}
 		}
 
 
 		return eventDefs;
 		return eventDefs;
-	},
-
-
-	standardPropMap: {}, // will be cloned by allowRawProps
-
-
-	/*
-	Returns true/false for success
-	*/
-	applyRawProps: function(rawProps) {
-		var standardPropMap = this.standardPropMap;
-		var manualProps = {};
-		var otherProps = {};
-		var propName;
-
-		for (propName in rawProps) {
-			if (standardPropMap[propName] === true) { // copy automatically
-				this[propName] = rawProps[propName];
-			}
-			else if (standardPropMap[propName] === false) {
-				manualProps[propName] = rawProps[propName];
-			}
-			else {
-				otherProps[propName] = rawProps[propName];
-			}
-		}
-
-		this.applyOtherRawProps(otherProps);
-
-		return this.applyManualRawProps(manualProps);
-	},
-
-
-	/*
-	If subclasses override, they must call this supermethod and return the boolean response.
-	*/
-	applyManualRawProps: function(rawProps) {
-		this.id = EventSource.normalizeId(rawProps.id);
-
-		if ($.isArray(rawProps.className)) {
-			this.className = rawProps.className;
-		}
-		if (typeof rawProps.className === 'string') {
-			this.className = rawProps.className.split(/\s+/);
-		}
-
-		return true;
-	},
-
-
-	applyOtherRawProps: function(rawProps) {
-		// subclasses can implement
 	}
 	}
 
 
 });
 });
 
 
 
 
+// finish initializing the mixin
+EventSource.allowRawProps = ParsableModelMixin_allowRawProps;
+
+
 EventSource.uuid = 0;
 EventSource.uuid = 0;
 
 
 
 
@@ -136,15 +88,6 @@ EventSource.normalizeId = function(id) {
 // ---------------------------------------------------------------------------------------------------------------------
 // ---------------------------------------------------------------------------------------------------------------------
 
 
 
 
-EventSource.allowRawProps = function(propDefs) {
-	var proto = this.prototype;
-
-	proto.standardPropMap = Object.create(proto.standardPropMap);
-
-	copyOwnProps(propDefs, proto.standardPropMap);
-};
-
-
 EventSource.allowRawProps({
 EventSource.allowRawProps({
 	// manually process...
 	// manually process...
 	id: false,
 	id: false,