1.0.0-alpha.14 • Published 6 years ago

@profiscience/knockout-contrib-utils-merge v1.0.0-alpha.14

Weekly downloads
1
License
WTFPL
Repository
github
Last release
6 years ago

@profiscience/knockout-contrib-utils-merge

Version Dependency Status Peer Dependency Status Dev Dependency Status Downloads

NOTE: It is recommended to use the @profiscience/knockout-contrib-utils metapackage

Usage

merge(dest, src, mapArrays = false)

For each enumerable property of src, a) creates an observable when undefined on dest b) updates when existing observable on dest c) sets when existing non-observable on dest.

If mapArrays 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 deep 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.

import { merge } from '@profiscience/knockout-contrib-utils'

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

merge(foos, {
  foo: 'new foo',
  bar: 'new bar',
  baz: 'baz'
})

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