2.0.6 • Published 4 years ago

@profiscience/knockout-contrib-utils-assign v2.0.6

Weekly downloads
2
License
WTFPL
Repository
github
Last release
4 years ago

utils.assign

Version Dependency Status Peer Dependency Status Dev Dependency Status Downloads

This package is intended for consumption via the @profiscience/knockout-contrib metapackage

(Deep) Object.assign(), but updates existing observables instead of overwriting them. Simply put, updates an observable tree by merging in new values.

Usage

assign(dest, src, options = { mapArrayElements: false, strict: false })

If mapArrayElements is true, array elements will be created as mapped observables, else bare objects/primitives. NOTE: Merging new arrays onto existing ones that have been mapped mapArrayElements will create new observable elements, not update the existing ones. No attempts are made to key elements, nor will they. If you need more, you probably want ko.mapping which is much more powerful, but far slower.

NOTE: Non-writable observables on the destination (computeds w/o write functions) will be silently ignored

import { assign } from '@profiscience/knockout-contrib'

const foos = {
  foo: ko.observable('foo'),
  bar: 'bar',
}

assign(foos, {
  foo: 'new foo',
  bar: 'new bar',
  baz: 'baz',
})
// {
//   foo: ko.observable('new foo'),
//   bar: 'new bar',
//   baz: ko.observable('baz')
// }

assign(
  foos,
  {
    foo: 'new foo',
    bar: 'new bar',
    baz: 'baz',
  },
  { strict: true }
)
// {
//   foo: ko.observable('new foo'),
//   bar: 'new bar',
//   baz: 'baz' <----------------------- When strict and observable is not pre-created on dest, property will NOT be observable
// }