README
property-manager
make it easier to manage the properties/attributes of your class.
Features:
- Inherited properties with class.
- Assign properties from a plain object(JSON Object).
- Clone object.
- Compare object whether is the same.
- Export properties to a plain object(JSON Object).
- Declare properties with type and default value.
- Supports
arrayOf
property with type - Supports property with
template
(the property value is determined by the template content):template
{string | (this) => string}:- the template string, eg,
'${author}-${uuid()}'
- or customize template function,
function() {return this.author + '-' + uuid()}
- the template string, eg,
imports
: {Object} the optional functions could be used in the template string.- NOTE: the template property is readonly by default. You can make it writeable. Once a new value has been written, the template will be no useful unless the new value is null or undefined.
- Supports
We often need to manage the properties of an object, consider the following:
- Set the options to the properties of object when the object was created
var myObj = new MyObject({opt1:value1, opt2:value2})
- Assign the options from another object:
myObj.assign({opt1:v1, opt2:v2})
- Clone an object:
var newObj = myObj.clone()
- Compare two objects whether is the same by their options(assigned properties).
myObj.isSame(anotherObj)
- Export the properties as a plain object or JSON to make recreate the object easier in the future.
- There are some internal attributes should not be exported.
- The empty or default value of an attribute should not be exported.
- the meaningful(non-english) name should be exported and assigned.
myObj.toObject()
andmyObj.toJSON()
JSON.stringify(myObj)
- Problem: how to assign an object value of a property?
- replace the standard
assignPropertyTo()
method. - define the attribute's
assign(value, dest, src, name)
method on the$attributes
.- the custom attribute's
assign
the value. return the changed value.
- the custom attribute's
- replace the standard
- Problem: how to decide which property should be assign or get default value of an attribute?
- define all attributes on this object even though the value is null.
- no default value feature.
- define a simple
$attributes
property to manage this:- {attrName: {value:'defaultValue'}, ...}
- define a complex
$attributes
(useProperties
class) to manage attributes.
- define all attributes on this object even though the value is null.
So we have these classes: SimplePropertyManager,NormalPropertyManager and AdvancePropertyManager.
first the rules of the properties:
- exported attributes means they are the JSON.stringify(aObj) attributes only.
- The non-enumerable attributes can not be exported and assigned.
- The enumerable attributes beginning with '