-
Notifications
You must be signed in to change notification settings - Fork 1
model
Sudo.js model classes store, retrieve and delete data via simple keys, as well as (dot delimited) 'paths'.
The data in question is always located in an internal object literal located at this.data
. Having a central location for all operations makes many useful features possible. From sensible serialization to data-binding.
###get(key)
Fetch and return the value located at "key". You need not worry if the key exists or not, "undefined" is returned in that case.
model.get('foo');
// => returns the value at data: {foo: 'value'}
###getPath(path)
Fetch and return the value located at a key inside a series of nested objects. You need not worry if all the path parts exist, "undefined" will be returned in that case.
model.getPath('foo.bar.baz');
// => traverses the path data: { foo: { bar: { baz: 'value'}}}
// => and returns 'value'
###gets(ary)
Passed an array of string "keys" or "paths" (or a mixture of both) return an object literal containing the "keys" | "paths" passed in with their associated values in this model. This is achieved by calling either get or getPath (the string contains a '.' character- this is worth noting) with each element in the array passed in.
// data: {foo: 'qux', bar: {baz: 'vot'}}
model.gets(['foo', 'bar.baz']);
// => {foo: 'qux', 'bar.baz': 'vot'}
###set(key, value)
Set the value at "key", returns this
. The "key" must be a string but the "value" can be any legal Javascript object type.
model.set('foo', 'bar');
// => data: {foo: 'bar'}
###setPath(path, value)
Set the value at "path", returns this
. The "path" must be a dot-delimited string but the "value" can be any legal Javascript object type. All the path pieces do not need to exist before setting. For example in the following snippet the "bar" object would be created if it did not exist.
model.set('foo.bar', 'baz');
// => data: {foo: {bar: 'baz'}
###sets(obj)
Given a object literal, set the "key", or "path" and the associated value in this model. Works by calling either set or setPath (contains a '.' character) for each key:value pair in the given hash.
// data: {}
model.sets({foo: 'bar', 'baz.quux': 'vot'});
// => data: {foo: 'bar', baz: {quux: 'vot'}}
###unset(key)
Remove the value located at "key". Does nothing if the key does not exist, returns this
.
// data: {foo: 'bar', baz: 'qux'}
model.unset('baz');
// => data: {foo: 'bar'}
###unsetPath(path)
Remove the value located at "path". Does nothing if the defined path is not found, returns this
.
// data: {foo: {bar: 'baz', qux: 'vot'}}
model.unsetPath('foo.bar');
// => data: {foo: {qux: 'vot'}}
###unsets(ary)
Removes multiple key:value (or path:value) pairs from this model. Uses either unset or unsetPath for each element of the array passed. Returns this.
// => data: {foo: 'bar', baz: {quux: 'vot', gob: 'zim'}}
model.unsets(['foo', 'baz.quux']);
// => data: {baz: {gob: 'zim'}}
When extended via the Observable Extension data binding (or data-mutation-observation) is possible via observe, and when extended via the Persistable Extension restful CRUD operations are abstracted into create, read, update and destroy.