PropDefinition
Defines the type, initial value, and get, set, and serialize behavior for an
observable property. These behaviors can be specified with as an Object
, String
,
Constructor
function, Array
, a getter expression
, or setter expression
.
Object
Defines multiple behaviors for a single property.
{
propertyName: {
default: function() { /* ... */ },
Default: Constructor,
type: function() { /* ... */ },
Type: Constructor,
get: function() { /* ... */ },
value: function() { /* ... */ },
set: function() { /* ... */ },
serialize: function() { /* ... */ },
identity: Boolean
}
}
function()
Either creates a method or Defines a Type setting with a constructor function. Constructor functions are identified with isConstructorLike.
{
propertyName: Constructor
}
OR
{
propertyName: function() {}
}
For example:
{
subMap: DefineMap // <- sets Type to DefineMap
}
OR
{
increment: function() {
++this.count;
} // <- sets method prop
}
Array
Defines an inline can-define/list/list Type setting. This is used as a shorthand for creating a property that is an can-define/list/list of another type.
{
propertyName: [Constructor | propDefinitions]
}
For example:
{
users: [ User ],
todos: [ { complete: "boolean", name: "string" } ]
}
GETTER
Defines a property's get behavior with the syntax.
{
get propertyName() { /* ... */ }
}
For example:
{
get fullName() {
return this.first + " " + this.last;
}
}
This is a shorthand for providing an object with a get
property like:
{
fullName: {
get: function() {
return this.first + " " + this.last;
}
}
}
You must use an object with a get property if you want your get to take the lastSetValue
or resolve
arguments.
SETTER
Defines a property's set behavior with the set syntax.
{
set propertyName( newValue ) { /* ... */ }
}
For example:
{
set fullName( newValue ) {
const parts = newVal.split( " " );
this.first = parts[ 0 ];
this.last = parts[ 1 ];
}
}
This is a shorthand for providing an object with a set
property like:
{
fullName: {
set: function(newValue){
var parts = newVal.split(" ");
this.first = parts[0];
this.last = parts[1];
}
}
}
You must use an object with a set property if you want your set to take the resolve
argument.
Use
A property definition can be defined in several ways. The Object
form is the most literal
and directly represents a PropDefinition
object. The other forms
get converted to a PropDefinition
as follows:
DefineMap.extend( {
propertyA: Object, // -> PropertyDefinition
propertyB: String, // -> {type: String}
propertyC: Constructor, // -> {Type: Constructor}
propertyD: [ PropDefs ], // -> {Type: DefineList.extend({"#": PropDefs})>}
get propertyE() { /* ... */ }, // -> {get: propertyE(){ /* ... */ }}
set propertyF( value ) { /* ... */ }, // -> {set: propertyF(value){ /* ... */ }}
method: Function
} );
Within a property definition, the available properties and their signatures look like:
DefineMap.extend({
property: {
get: function(lastSetValue, resolve){...},
set: function(newValue, resolve){...},
type: function(newValue, prop){...}| Array<PropertyDefinition> | PropertyDefinition,
Type: Constructor | Array<PropertyDefinition> | PropertyDefinition,
default: function(){...},
Default: Constructor,
serialize: Boolean | function(){...}
}
})
For example:
const Person = DefineMap.extend( "Person", {
// a `DefineList` of `Address`
addresses: [ Address ],
// A `DefineMap` with a `first` and `last` property
name: { type: { first: "string", last: "string" } },
// A `DefineList of a ``DefineMap` with a `make` and `year` property.
cars: { Type: [ { make: "string", year: "number" } ] }
} );
const person = new Person( {
addresses: [ { street: "1134 Pinetree" } ],
name: { first: "Kath", last: "Iann" },
cars: [ { make: "Nissan", year: 2010 } ]
} );