Class: SGModel

SGModel(propsopt, thisPropsopt, optionsopt)

SGModel v1.0.3. Fast lightweight library (ES6) for structuring web applications using binding models and custom events. This is a faster and more simplified analogue of Backbone.js!

Constructor

new SGModel(propsopt, thisPropsopt, optionsopt)

SGModel constructor

Parameters:
Name Type Attributes Default Description
props object <optional>
{}

Properties

thisProps object <optional>
void 0

Properties and methods passed to the this context of the created instance

options object <optional>
void 0

Custom settings

License:
  • SGModel may be freely distributed under the MIT license
Source:
See:

Members

(static) defaultProperties

Properties default values

Source:

(static, constant) FLAG_IMMEDIATELY :boolean

The flag passed in the .on(...) call to execute the callback

Type:
  • boolean
Source:

(static) localStorageKey

If a non-empty string value is specified, then the data is synchronized with the local storage. Support for storing data as one instance of a class (single instance), and several instances: localStorageKey + "_" + id

Source:

(static) ownSetters

List of properties for which to use their own setters first Better than .on(...) for speed of work with a large number of class instances. Also used if there is a base class and a descendant class where specific behavior is needed when changing properties.

Source:
Example
...
static ownSetters = Object.assign({
	state: true
}, OurBaseModel.ownSetters);
...
setState(value, options = SGModel.OBJECT_EMPTY, flags = 0) {
	if (this.set("state", value, options, flags | SGModel.FLAG_IGNORE_OWN_SETTER)) {
		//some code...
	}
}

(static) singleInstance

Enable singleton pattern for model

Source:

(static) typeProperties

Property data types

Source:

(static, readonly) version

Library version (fixed in minified version)

Source:

Methods

(static) clone(source) → {object|primitive}

Full cloning (with nested objects). Attention! There is no check for circular references. You cannot allow nested objects to refer to each other through properties, because recursion is used!

Parameters:
Name Type Description
source object | primitive
Source:
Returns:
Type
object | primitive

(static) defaults(dest, sources)

If dest does not have a property from sources, then it is copied from sources to dest (composite objects are copied completely using recursion!)

Parameters:
Name Type Description
dest object
sources object
Source:

(static) get()

Method get() for single instance of a class

Source:

(static) getInstance()

Source:

(static) getProperties()

Method getProperties() for single instance of a class

Source:

(static) initObjectByObject(dest, source) → {dest}

Fill the values dest with the values from source (with recursion). If there is no property in source, then it is ignored for dest

Parameters:
Name Type Description
dest object | array
source object | array
Source:
Returns:
Type
dest

(static) off()

Method off()* for single instance of a class

Source:

(static) on()

Method on() for single instance of a class

Source:

(static) roundTo(value, precision) → {Number}

Rounding to the required precision

Parameters:
Name Type Description
value Number
precision Number
Source:
Returns:
Type
Number

(static) save()

Method save() for single instance of a class

Source:

(static) set()

Method set() for single instance of a class

Source:

(static) toBoolean()

Source:

(static) upperFirstLetter()

Source:

defaults() → {object}

Sets the default property values. Overriden

Source:
Returns:
Type
object

destroy()

Destroy the instance

Source:

get()

Get property value

Source:

has()

Check if there is a property in the model

Source:

initialize()

Called when an instance is created. Override in your classes

Source:

off(name, func)

Remove trigger on property change

Parameters:
Name Type Description
name string | array
func function
Source:

on(name, func, context, data, flags)

Set trigger for property change

Parameters:
Name Type Default Description
name string | array
func function
context object

If not specified, the this of the current object is passed

data mixed

If data is set, then this value (data) is passed in the first arguments [] callback

flags number 0

Valid flags: SGModel.FLAG_IMMEDIATELY - func will be executed once now

Source:

save()

Save instance data to local storage

Source:

set(name, val, optionsopt, flagsopt) → {boolean}

Set property value

Parameters:
Name Type Attributes Default Description
name string
val mixed
options object <optional>
void 0
Properties
Name Type Attributes Description
precision number <optional>

Rounding precision

previous_value mixed <optional>

Use this value as the previous value

flags number <optional>
0

Valid flags: FLAG_OFF_MAY_BE | FLAG_PREV_VALUE_CLONE | FLAG_NO_CALLBACKS | FLAG_FORCE_CALLBACKS | FLAG_IGNORE_OWN_SETTER

Source:
Returns:

If the value was changed will return true

Type
boolean

setOnAllCallback(func, flags)

Set trigger to change any property

Parameters:
Name Type Default Description
func function
flags number 0

Valid flags: SGModel.FLAG_IMMEDIATELY - func will be executed once now

Source:

trigger(name, flags)

Execute callbacks that are executed when the property value changes

Parameters:
Name Type Default Description
name string
flags number 0

Valid flags: SGModel.FLAG_OFF_MAY_BE - if set can be .off(), then you need to pass this flag

Source: