Skip to content

Commit

Permalink
Pulling out serialization formats into module extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gburghardt committed Jul 10, 2012
1 parent 42e80fe commit c0588e6
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 150 deletions.
150 changes: 0 additions & 150 deletions src/lib/modules/models/BaseModel/serialization.js
Expand Up @@ -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]) + "</" + key + ">");
}
}

moduleCallbacksResult = this.applyModuleCallbacks("toXml", [options]);

if (moduleCallbacksResult) {
xml.push(moduleCallbacksResult.join(""));
}

if (options.rootElement) {
xml.push("</" + options.rootElement.replace(/\[/g, ":").replace(/\]/g, "") + ">");
}
}

return xml.join(glue);
}

}
Expand Down
47 changes: 47 additions & 0 deletions 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;
}

}

});
50 changes: 50 additions & 0 deletions 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("&");
}

}

});
74 changes: 74 additions & 0 deletions 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]) + "</" + key + ">");
}
}

moduleCallbacksResult = this.applyModuleCallbacks("toXml", [options]);

if (moduleCallbacksResult) {
xml.push(moduleCallbacksResult.join(""));
}

if (options.rootElement) {
xml.push("</" + options.rootElement.replace(/\[/g, ":").replace(/\]/g, "") + ">");
}
}

return xml.join(glue);
}

}

});
3 changes: 3 additions & 0 deletions test/framework/models/BaseModel.spec.html
Expand Up @@ -17,6 +17,9 @@
<script type="text/javascript" src="../../../src/lib/modules/models/BaseModel/basicValidation.js"></script>
<script type="text/javascript" src="../../../src/lib/modules/models/BaseModel/extendedValidation.js"></script>
<script type="text/javascript" src="../../../src/lib/modules/models/BaseModel/serialization.js"></script>
<script type="text/javascript" src="../../../src/lib/modules/models/BaseModel/serialization.queryString.js"></script>
<script type="text/javascript" src="../../../src/lib/modules/models/BaseModel/serialization.json.js"></script>
<script type="text/javascript" src="../../../src/lib/modules/models/BaseModel/serialization.xml.js"></script>
<script type="text/javascript" src="../../../src/lib/modules/models/BaseModel/relations.js"></script>

<!-- include spec files here... -->
Expand Down

0 comments on commit c0588e6

Please sign in to comment.