wu

Reactive, functional and declarative framework for webapps

View the Project on GitHub migueldelmazo/wu

Wu framework: public model methods

The model of Wu is simply a plain object of Javascript where you can set and get the properties with the values you want.

It is a reactive model, you can watch one or several properties of the model. When someone changes one of those properties you will be notified.

It is a preventive model. If you set a property with the same value several times, you will only be notified the first time.

With the validators of the model you can decide when to be notified of a change.

Public model methods

wu.model.set()

Set a value in a property of the data model.

Arguments:

Example of use:

wu.model.set('user.profile.name', 'Anna')

wu.model.get()

Return the value of a property of the data model. The value is cloned before being returned so that the data model can not be modified by mistake.

Arguments:

Return:

Example of use:

wu.model.get('user.profile.name', '') // 'Anna'

wu.model.populate()

Iterate and return an object and if its values are a strings, it parses them with the values of the data model. If the string starts with # do not parse it.

Arguments:

Return:

Example of use:

wu.model.populate('user.profile.name') // 'Anna'
wu.model.populate({ email: 'user.profile.name' }) // { email: 'Anna' }
wu.model.populate({ email: 'user.profile.name' }) // { email: 'Anna' }
wu.model.populate({ email: 'user.profile.name', foo: '#user.profile.name' }) // { email: 'Anna', foo: 'user.profile.name' }
wu.model.populate({ email: 'user.profile.name', number: 123 }) // { email: 'Anna', number: 123 }
wu.model.populate(['user.profile.name']) // ['Anna']
wu.model.populate(['user.profile.name', '#user.profile.name']) // ['Anna', 'user.profile.name']
wu.model.populate(['user.profile.name', 1, true, null, undefined]) // ['Anna', 1, true, null, undefined]

wu.model.watch()

Start watching some paths of the data model and run the functions when the values change.

Arguments:

Return:

Example of use:

wu.model.watch(
  'user.id',
  () => {
    const userId = wu.model.get('user.id')
    window.localStorage('userId', userId)
  },
  {
    'user.name': [_.negate(_.isEmpty), _.isString],
    'user.age': _.isNumber,
    'user.status': (status) => status === 'ready'
  }
)

wu.model.stopWatching()

Stop watching a path that has previously been watched by wu.watch. Requires the identifier returned by wu.watch.

Arguments:

Example of use:

const watcherId = wu.model.watch('user.id', myFunction)
wu.model.stopWatching(watcherId)