San provides a number of methods on components’ data. You can retrieve data via .get()
method and modify data via .set()
, .splice()
methods, views will be updated automatically.
Note
: You may ask why do we have to use these methods instead of manipulating data directly? That’s because the defineProperty API is still not fully supported by major browsers in China and we prefer non-invasive ways to implement data binding. As a result, you have to use the methods provided by San in order to get the views updated automatically.
Initialization
By defining an initData
method, you can specify the data used to initialize the component. The return value of initData
method will be used as the initialization data.
1 | san.defineComponent({ |
get
1 | {*} get({string|Object?}expr) |
Explanation
:
Use .get()
method to retrieve data.
1 | san.defineComponent({ |
The entire data object will be returned if the expr
argument is not provided. The no argument function overload is provided for ESNext destructuring to retrieve multiple data items with a single expression.
1 | san.defineComponent({ |
Note
: The data retrieved via get method is not expected to be modified, which will lead to inconsistent state. Use the following set, splice methods to modify any data.
set
1 | set({string|Object}expr, {*}value, {Object?}option) |
Explanation
:
The .set()
method should be used in most cases, which is equivalent to the JavaScript assignment operator (=).
Usage
:
1 | san.defineComponent({ |
assign
1 | assign({Object}source, {Object?}option) |
Version
: >= 3.9.0
Explanation
:
assign
method merges source
object into component’s data, which is similar to Object.assign
in JavaScript, this makes a batch update to view.
Usage
:
1 | san.defineComponent({ |
merge
1 | merge({string|Object}expr, {Object}source, {Object?}option) |
Version
: >= 3.4.0
Explanation
:
The .merge()
method merges source
into the data item specified by expr
.
Usage
:
1 | san.defineComponent({ |
apply
1 | apply({string|Object}expr, {function({*}):{*}}value, {Object?}option) |
Version
: >= 3.4.0
Explanation
:
.apply()
method accepts a function argument, which takes the data’s current value as its input, and returns the updated value. The behavior of .apply()
is like the JavaScript Array.prototype.map
method.
Usage
:
1 | san.defineComponent({ |
Array Methods
We provide a number of methods for array manipulation. All of them take the same name as the Array.prototype
except remove. Hope it helps to memorize!
Note
: Use .set()
method to modify an Array element. Actually whenever you want a JavaScript =
assignment, you’ll need the .set()
method.
1 | san.defineComponent({ |
push
1 | {number} push({string|Object}expr, {*}value, {Object?}option) |
Explanation
:
Append an element into an array.
Usage
:
1 | san.defineComponent({ |
pop
1 | {*} pop({string|Object}expr, {Object?}option) |
Explanation
:
Pop one element out of an array.
Usage
:
1 | san.defineComponent({ |
unshift
1 | {number} unshift({string|Object}expr, {*}value, {Object?}option) |
Explanation
:
Insert one element to the beginning of an array.
Usage
:
1 | san.defineComponent({ |
shift
1 | {*} shift({string|Object}expr, {Object?}option) |
Explanation
:
Remove one element from the beginning of an array.
Usage
:
1 | san.defineComponent({ |
remove
1 | remove({string|Object}expr, {*}item, {Object?}option) |
Explanation
:
Remove the given item.
.remove()
compares item
to elements of the Array using strict equality (the same method used by the ===
operator).
Usage
:
1 | san.defineComponent({ |
removeAt
1 | removeAt({string|Object}expr, {number}index, {Object?}option) |
Explanation
:
Remove the element at the given index.
Usage
:
1 | san.defineComponent({ |
splice
1 | {Array} splice({string|Object}expr, {Array}spliceArgs, {Object?}option) |
Explanation
:
The .splice()
method changes the contents of an array by removing existing elements and/or adding new elements.
Usage
:
1 | san.defineComponent({ |
option
Each of the above methods accepts an extra option
argument, which is a plain object of key-value pairs to control how the views will be updated.
silent
Explanation
:
Update the data silently without updating the view.
force
Version
: >= 3.5.5
Explanation
:
Force update the view even if the data to be set is identical to the existing value. Available for .set()
method only.
Data Validation
Validation rules can be specified for component data. san will throw the corresponding error when validation fails. It’s rather useful in collaboration.
Use DataTypes
to declare validation rules:
1 | import san, {DataTypes} from 'san'; |
DataTypes
provides a series of validators to ensure the data received is valid. In the above example, a DataTypes.string
validator is used so that san will throw an error when the value for name
is not a String
.
For performance considerations, dataTypes
only get evaluated in development
environment.
Please refer to this link to check out their availabilities in different san releases.
DataTypes
Following is a demo for a variety of DataTypes
validators:
1 | import san, {DataTypes} from 'san'; |