Version 1.5 glow.data
API Quick Reference
JavaScript is required to use the quick reference
Serialising and de-serialising data
Further Info & Examples
Methods
- decodeJson
-
Decodes a string JSON representation into an object.
Synopsis
glow.data.decodeJson(string, opts);
Parameters
- string
-
- Type
The string to be decoded. Must be valid JSON.
- opts
-
- Type
Zero or more of the following as properties of an object:
- safeMode
-
Whether the string should only be decoded if it is deemed "safe".
- Type
- Default
- false
- Optional
- Yes
The json.org regular expression checks are used.
Returns
Description
Returns a JavaScript object that mirrors the data given.
Example
var getRef = glow.data.decodeJson('{foo: "Foo", bar: ["Bar 1", "Bar2"]}'); // will return {foo: "Foo", bar: ["Bar 1", "Bar2"]} var getRef = glow.data.decodeJson('foobar', {safeMode: true}); // will throw an error
- decodeUrl
-
Decodes a query string into an object.
Synopsis
glow.data.decodeUrl(string);
Parameters
- string
-
- Type
The query string to be decoded.
It should not include the initial question mark.
Returns
Description
Returns an object representing the data given by the query string, with all values suitably unescaped. All keys in the query string are keys of the object. Repeated keys result in an array.
Example
var getRef = glow.data.decodeUrl("foo=Foo&bar=Bar%201&bar=Bar2"); // will return the object {foo: "Foo", bar: ["Bar 1", "Bar2"]}
- encodeJson
-
Encodes an object into a string JSON representation.
Synopsis
glow.data.encodeJson(object);
Parameters
- object
-
- Type
The object to be encoded.
This can be arbitrarily nested, but must not contain functions or cyclical structures.
Returns
Description
Returns a string representing the object as JSON.
Example
var myObj = {foo: "Foo", bar: ["Bar 1", "Bar2"]}; var getRef = glow.data.encodeJson(myObj); // will return '{"foo": "Foo", "bar": ["Bar 1", "Bar2"]}'
- encodeUrl
-
Encodes an object for use as a query string.
Synopsis
glow.data.encodeUrl(object);
Parameters
- object
-
- Type
The object to be encoded.
This must be a hash whose values can only be primitives or arrays of primitives.
Returns
Description
Returns a string representing the object suitable for use as a query string, with all values suitably escaped. It does not include the initial question mark. Where the input field was an array, the key is repeated in the output.
Example
var getRef = glow.data.encodeUrl({foo: "Foo", bar: ["Bar 1", "Bar2"]}); // will return "foo=Foo&bar=Bar%201&bar=Bar2"
- escapeHTML
-
Escape HTML entities.
Synopsis
glow.data.escapeHTML(string);
Parameters
- string
-
- Type
The string to be escaped.
Returns
Description
Returns a string with HTML entities escaped.
Example
// useful for protecting against XSS attacks: var fieldName = '" onclick="alert(\'hacked\')" name="'; // but should be used in all cases like this: glow.dom.create('<input name="' + glow.data.escapeHTML(untrustedString) + '"/>');