From c0588e6fac4c91e7a87201cd2751f06c208bdbac Mon Sep 17 00:00:00 2001 From: Greg Burghardt Date: Tue, 10 Jul 2012 18:17:48 -0500 Subject: [PATCH] Pulling out serialization formats into module extensions. --- .../modules/models/BaseModel/serialization.js | 150 ------------------ .../models/BaseModel/serialization.json.js | 47 ++++++ .../BaseModel/serialization.queryString.js | 50 ++++++ .../models/BaseModel/serialization.xml.js | 74 +++++++++ test/framework/models/BaseModel.spec.html | 3 + 5 files changed, 174 insertions(+), 150 deletions(-) create mode 100644 src/lib/modules/models/BaseModel/serialization.json.js create mode 100644 src/lib/modules/models/BaseModel/serialization.queryString.js create mode 100644 src/lib/modules/models/BaseModel/serialization.xml.js diff --git a/src/lib/modules/models/BaseModel/serialization.js b/src/lib/modules/models/BaseModel/serialization.js index a6f4978..bf8c9e2 100644 --- a/src/lib/modules/models/BaseModel/serialization.js +++ b/src/lib/modules/models/BaseModel/serialization.js @@ -16,156 +16,6 @@ BaseModel.includeModule("serialization", { var methodName = "to" + type.capitalize(); var x = (!!this[methodName]) ? this[methodName](options) : this.toQueryString(options); return x; - }, - - toJson: function(options) { - options = options || {}; - var json = "", moduleCallbacksResult, attrs = {}, i, length, key; - - if (options.rootElement) { - json += '{"' + options.rootElement + '":'; - } - - if (options.changedAttributesOnly) { - for (key in this._changedAttributes) { - if (this._changedAttributes.hasOwnProperty(key) && this._changedAttributes[key]) { - attrs[key] = this._attributes[key]; - } - } - - attrs[this.primaryKey] = this.attributes[this.primaryKey]; - } - else { - length = this.validAttributes.length; - - for (i = 0; i < length; i++) { - key = this.validAttributes[i]; - attrs[key] = this._attributes[key]; - } - } - - json += JSON.stringify(attrs); - moduleCallbacksResult = this.applyModuleCallbacks("toJson", [options]); - - if (moduleCallbacksResult.length) { - json += "," + moduleCallbacksResult.join(""); - } - - if (options.rootElement) { - json += '}'; - } - - return json; - }, - - toQueryString: function(options) { - options = options || {}; - var attrs = null, key, queryString = [], moduleCallbacksResult; - - if (options.changedAttributesOnly) { - attrs = {}; - - for (key in this.changedAttributes) { - if (this._changedAttributes.hasOwnProperty(key) && this._changedAttributes[key]) { - attrs[key] = this._attributes[key]; - } - } - - attrs[this.primaryKey] = this.attributes[this.primaryKey]; - } - else { - attrs = this._attributes; - } - - if (options.rootElement) { - for (key in attrs) { - if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { - queryString.push(options.rootElement + "[" + escape(key) + "]=" + escape(attrs[key])); - } - } - } - else { - for (key in attrs) { - if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { - queryString.push(escape(key) + "=" + escape(attrs[key])); - } - } - } - - moduleCallbacksResult = this.applyModuleCallbacks("toQueryString", [options]); - - if (moduleCallbacksResult.length) { - queryString.push(moduleCallbacksResult.join("")); - } - - return queryString.join("&"); - }, - - toXml: function(options) { - options = options || {}; - var attrs, key, xml = [], glue = "", moduleCallbacksResult; - - if (options.changedAttributesOnly) { - attrs = {}; - - for (key in this._changedAttributes) { - if (this._changedAttributes.hasOwnProperty(key) && this._changedAttributes[key]) { - attrs[key] = this._attributes[key]; - } - } - - attrs[this.primaryKey] = this.attributes[this.primaryKey]; - } - else { - attrs = this._attributes; - } - - if (options.shorthand) { - if (!options.rootElement) { - throw new Error("options.rootElement is required when converting to XML using shorthand format."); - } - - xml.push("<" + options.rootElement); - - for (key in attrs) { - if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { - xml.push(key + '="' + this.escapeHTML(attrs[key]) + '"'); - } - } - - xml.push("/>"); - - moduleCallbacksResult = this.applyModuleCallbacks("toXml", [options]); - - if (moduleCallbacksResult.length) { - xml.push(moduleCallbacksResult.join("")); - } - - glue = " "; - } - else { - if (options.rootElement) { - xml.push("<" + options.rootElement.replace(/\[/g, ":").replace(/\]/g, "") + ">"); - } - - for (key in attrs) { - if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { - xml.push("<" + key + ">" + this.escapeHTML(attrs[key]) + ""); - } - } - - moduleCallbacksResult = this.applyModuleCallbacks("toXml", [options]); - - if (moduleCallbacksResult) { - xml.push(moduleCallbacksResult.join("")); - } - - if (options.rootElement) { - xml.push(""); - } - } - - return xml.join(glue); } } diff --git a/src/lib/modules/models/BaseModel/serialization.json.js b/src/lib/modules/models/BaseModel/serialization.json.js new file mode 100644 index 0000000..929af24 --- /dev/null +++ b/src/lib/modules/models/BaseModel/serialization.json.js @@ -0,0 +1,47 @@ +BaseModel.extendModule("serialization", { + + prototype: { + + toJson: function(options) { + options = options || {}; + var json = "", moduleCallbacksResult, attrs = {}, i, length, key; + + if (options.rootElement) { + json += '{"' + options.rootElement + '":'; + } + + if (options.changedAttributesOnly) { + for (key in this._changedAttributes) { + if (this._changedAttributes.hasOwnProperty(key) && this._changedAttributes[key]) { + attrs[key] = this._attributes[key]; + } + } + + attrs[this.primaryKey] = this.attributes[this.primaryKey]; + } + else { + length = this.validAttributes.length; + + for (i = 0; i < length; i++) { + key = this.validAttributes[i]; + attrs[key] = this._attributes[key]; + } + } + + json += JSON.stringify(attrs); + moduleCallbacksResult = this.applyModuleCallbacks("toJson", [options]); + + if (moduleCallbacksResult.length) { + json += "," + moduleCallbacksResult.join(""); + } + + if (options.rootElement) { + json += '}'; + } + + return json; + } + + } + +}); diff --git a/src/lib/modules/models/BaseModel/serialization.queryString.js b/src/lib/modules/models/BaseModel/serialization.queryString.js new file mode 100644 index 0000000..15be61b --- /dev/null +++ b/src/lib/modules/models/BaseModel/serialization.queryString.js @@ -0,0 +1,50 @@ +BaseModel.extendModule("serialization", { + + prototype: { + + toQueryString: function(options) { + options = options || {}; + var attrs = null, key, queryString = [], moduleCallbacksResult; + + if (options.changedAttributesOnly) { + attrs = {}; + + for (key in this.changedAttributes) { + if (this._changedAttributes.hasOwnProperty(key) && this._changedAttributes[key]) { + attrs[key] = this._attributes[key]; + } + } + + attrs[this.primaryKey] = this.attributes[this.primaryKey]; + } + else { + attrs = this._attributes; + } + + if (options.rootElement) { + for (key in attrs) { + if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { + queryString.push(options.rootElement + "[" + escape(key) + "]=" + escape(attrs[key])); + } + } + } + else { + for (key in attrs) { + if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { + queryString.push(escape(key) + "=" + escape(attrs[key])); + } + } + } + + moduleCallbacksResult = this.applyModuleCallbacks("toQueryString", [options]); + + if (moduleCallbacksResult.length) { + queryString.push(moduleCallbacksResult.join("")); + } + + return queryString.join("&"); + } + + } + +}); diff --git a/src/lib/modules/models/BaseModel/serialization.xml.js b/src/lib/modules/models/BaseModel/serialization.xml.js new file mode 100644 index 0000000..e17d323 --- /dev/null +++ b/src/lib/modules/models/BaseModel/serialization.xml.js @@ -0,0 +1,74 @@ +BaseModel.extendModule("serialization", { + + prototype: { + + toXml: function(options) { + options = options || {}; + var attrs, key, xml = [], glue = "", moduleCallbacksResult; + + if (options.changedAttributesOnly) { + attrs = {}; + + for (key in this._changedAttributes) { + if (this._changedAttributes.hasOwnProperty(key) && this._changedAttributes[key]) { + attrs[key] = this._attributes[key]; + } + } + + attrs[this.primaryKey] = this.attributes[this.primaryKey]; + } + else { + attrs = this._attributes; + } + + if (options.shorthand) { + if (!options.rootElement) { + throw new Error("options.rootElement is required when converting to XML using shorthand format."); + } + + xml.push("<" + options.rootElement); + + for (key in attrs) { + if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { + xml.push(key + '="' + this.escapeHTML(attrs[key]) + '"'); + } + } + + xml.push("/>"); + + moduleCallbacksResult = this.applyModuleCallbacks("toXml", [options]); + + if (moduleCallbacksResult.length) { + xml.push(moduleCallbacksResult.join("")); + } + + glue = " "; + } + else { + if (options.rootElement) { + xml.push("<" + options.rootElement.replace(/\[/g, ":").replace(/\]/g, "") + ">"); + } + + for (key in attrs) { + if (attrs.hasOwnProperty(key) && !this.valueIsEmpty(attrs[key])) { + xml.push("<" + key + ">" + this.escapeHTML(attrs[key]) + ""); + } + } + + moduleCallbacksResult = this.applyModuleCallbacks("toXml", [options]); + + if (moduleCallbacksResult) { + xml.push(moduleCallbacksResult.join("")); + } + + if (options.rootElement) { + xml.push(""); + } + } + + return xml.join(glue); + } + + } + +}); diff --git a/test/framework/models/BaseModel.spec.html b/test/framework/models/BaseModel.spec.html index ac3e697..5f855fa 100644 --- a/test/framework/models/BaseModel.spec.html +++ b/test/framework/models/BaseModel.spec.html @@ -17,6 +17,9 @@ + + +