new SmartModel(data, options, modelStructure) → {object}
Parameters:
| Name | Type | Description |
|---|---|---|
data |
object | If passed all the properties of the Model is assigned the value of property with same name in the data object passed. |
options |
object | Object with options to control the way data object's properties is passed into Model. |
modelStructure |
object | The structure of the Model to create. |
- Version:
- 1.0.0.3, 2019.03.03
- Source:
Returns:
The model instance.
- Type
- object
Methods
clean(propertiesopt)
Clean the status of all dirty properties and set current status as the "valid" status, setting
__isDirty to false and accepting every properties value. If the properties parameter is passed only some properties will be cleaned and the __isDirty status of the Model may remain true.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
properties |
array |
<optional> |
If an array of property path, as string, is passed, only that properties is cleaned. If not all the dirty properties are cleaned than the __isDirty property of the Model remains true. |
- Version:
- 1.0.0.1, 2019.03.03
- Source:
emit(eventName, eventDataopt)
Makes this Model dispatch an event.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
eventName |
string | The name of the event that will be emitted. | |
eventData |
object |
<optional> |
The data that must be passed with the event. It could be retrieved by the listener accepting a second parameter after the "event". |
- Version:
- 1.0.0.1, 2019.03.03
- Source:
export(optionsopt)
Export all properties from this Model to a generic JavaScript object that can be used to save the Model's data to localStorage o by server-side API.
The default behaviour of this method is to return an object with every property that name doesn't start with double underscore ("__"); no function is exported.
You can override this method to control wich data is exported and in what form and structure. If you want override this method, you can use the "private"
The default behaviour of this method is to return an object with every property that name doesn't start with double underscore ("__"); no function is exported.
You can override this method to control wich data is exported and in what form and structure. If you want override this method, you can use the "private"
__export method that will give you an object with defautl export result.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
options |
object |
<optional> |
By default this parameter is unused. If you override it you can choose to have something in to change the behaviour of your method. You can pass a string to change the type of export; in a User model case, for example, you cold pass a "login" value to only retrieve the eMail and password property, vs a "register" value that will export all user property. |
- Version:
- 1.0.0.1, 2019.03.03
- Source:
import(dataopt, optionsopt)
Takes in a generic JavaScript object or another Model and assign to the properties of this Model the values of the corresponding properties in given data object.
This method is also used by default creator of a Model when using Shark.getInstance() method and pass in a second parameter with data. So overriding this method you can change the default behaviour of your Model when some data are passed in at instantiation moment, and do any parsing or change you need.
If you override this method you can call the "private"
This method is also used by default creator of a Model when using Shark.getInstance() method and pass in a second parameter with data. So overriding this method you can change the default behaviour of your Model when some data are passed in at instantiation moment, and do any parsing or change you need.
If you override this method you can call the "private"
__import method that will do the default import so you can do only the changes or parse you need.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
data |
object |
<optional> |
The object with data that should be copied on this Model. |
options |
object |
<optional> |
- |
- Version:
- 1.0.0.1, 2019.03.03
- Source:
isDirty() → {boolean}
Used to find if some properties of this Model is changed since the first assignment (usually the one assigned during instantiation) or the value they have at the moment you call the
clean method.
- Version:
- 1.0.0.1, 2019.03.03
- Source:
Returns:
- Type
- boolean
restore(propertiesopt)
Restore all original values for dirty properties and set
__isDirty to false. If the proprerty parameter is passed some property may not be restored.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
properties |
array |
<optional> |
If an array of property path, as string, is passed, only that properties is restored. If not all the dirty properties are restored than the __isDirty property of the Model remains true. |
- Version:
- 1.0.0.1, 2019.03.03
- Source:
set(name, valueopt)
One of the main feature of the SmartModel, responsible for event dispatching on properties value changes and for maganage the dirty status of the model.
There is a lot of automation behind this method: auto-parsing is done, based on the type of property on the model and, if the passed value is different from the actual value, two events are dispatched (dataChanged and propertyNameChanged) and the original value is stored as a dirty property. Also the
Auto Parse
If the property that is been setted has a strong type (string, number, array) than the passed value is casted or converted to that type. For array type a different parse is done: if passed value is an array, then the value is used as is. But if the passed value is of primitive type, then a toggle check is made and the value is added or removed from the property array.
On value changes, also, a check is made for the presence of two function on the Model: "onDataChanged" and "onPropertyNameChanged". If one or both of these functions exists then they are called and passed the change data.
__isDirty property of the Model is set to true.
Auto Parse
If the property that is been setted has a strong type (string, number, array) than the passed value is casted or converted to that type. For array type a different parse is done: if passed value is an array, then the value is used as is. But if the passed value is of primitive type, then a toggle check is made and the value is added or removed from the property array.
On value changes, also, a check is made for the presence of two function on the Model: "onDataChanged" and "onPropertyNameChanged". If one or both of these functions exists then they are called and passed the change data.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
name |
string | The name of the property that will be assigned the value. | |
value |
any |
<optional> |
The value that will be assigned. |
- Version:
- 1.0.0.1, 2019.03.03
- Source:
Fires:
Events
onDataChanged
onDataChanged event is emitted every time a Model's property is setted a value different from the actual value.
Type:
- object
Properties:
| Name | Type | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
eventData |
object | An object containing information about the change of a property value.
Properties
|
- Source: