forked from gburghardt/JsLib
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulling out serialization formats into module extensions.
- Loading branch information
1 parent
42e80fe
commit c0588e6
Showing
5 changed files
with
174 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
50
src/lib/modules/models/BaseModel/serialization.queryString.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("&"); | ||
} | ||
|
||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters