DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
        • behaviors
          • ./base/
          • ./cache-requests/
          • ./can/constructor-hydrate/
          • ./can/map/
          • ./can/merge/
            • instance callbacks
              • createdInstance
              • destroyedInstance
              • updatedInstance
              • updatedList
          • ./can/ref/
          • ./constructor/callbacks-once/
          • ./constructor/
          • ./constructor/store/
          • ./data/callbacks/
          • ./data/callbacks-cache/
          • ./data/combine-requests/
          • ./data/localstorage-cache/
          • ./data/memory-cache/
          • ./data/parse/
          • ./data/url/
          • ./data/worker/
          • ./fall-through-cache/
          • ./real-time/
        • modules
          • ./can/base-map/
          • ./can/model/
          • ./can/super-map/
          • ./can/tag/
          • ./helpers/map-deep-merge
          • ./helpers/weak-reference-map
        • data types
          • DataInterface
          • Instance
          • InstanceInterface
          • List
          • ListData
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can/merge

  • Edit on GitHub

Minimally update nested data structures with the response from the server.

canMergeBehavior( baseConnection )

Overwrites can/map's instance callbacks so they use map-deep-merge. map-deep-merge is able to make minimal changes to the nested properties of can-define instances and lists given raw data. E.g:

var existingStudent;
var classroom = ClassRoom.get({id: 505}).then(function(instance) {
  instance.id; // 505
  instance.students[0].id; // 15
  instance.students[0].name; // 'Samantha Jones'
  existingStudent = instance;
});

... // later in the program new information for the classroom is retrieved

ClassRoom.get({id:505}).then(function(instance) {
    instance.id; // 505
    instance.students[0].id; // 15
    instance.students[0].name; // 'Samantha Jones-Baker'
    
    // true, if can-merge behavior is used. 
    // a new nested instance isn't created, instead it was updated with the changed fields
    existingStudent === instance.students[0]; 
});

To use can/merge, the connection's Map, List and any of their nested types must be properly configured. That configuration is discussed in the "Use" section below.

Parameters

  1. baseConnection {Object}:

    can-connect connection object that is having the can/merge behavior added on to it. Expects the can/map behavior to already be added to this base connection. If the connect helper is used to build the connection, the behaviors will automatically be ordered as required.

Returns

{Object}:

a can-connect connection containing the methods provided by can/merge.

Use

To use the can/merge behavior, you have to:

  1. Add the behavior after can/map, and
  2. Make sure all types, especially List type is properly configured.

Adding the can/merge behavior after can/map is pretty straightforward. When you create a custom connection, create it as follows:

var canMergeBehavior = require("can-connect/can/merge/merge");
var canMapBehavior = require("can-connect/can/map/map");

var ClassRoom = DefineMap.extend({
    ...
});

ClassRoom.List = DefineList.extend({
    "#": ClassRoom
});

ClassRoom.algebra = new set.Algebra({...})

ClassRoom.connection = connect([..., canMapBehavior, canMergeBehavior, ...],{
    Map: ClassRoom,
    List: ClassRoom.List
});

For map-deep-merge to merge correctly, it needs to know how to uniquely identify an instance and be able to convert raw data to instances and lists. map-deep-merge looks for this configuration on the .algebra and .connection properties of the Type setting on can-define types.

This is more easily understood in an example. If the ClassRoom has a students property that is a list of Student instances like:

var ClassRoom = DefineMap.extend({
    students: Student.List
});

To be able to uniquely identify Student instances within that list, make sure Student has an algebra property that is configured with the identifier property:

Student = DefineMap.extend({ ... });

Student.algebra = new set.Algebra(set.props.id("_id"))

Also make sure that Student.List points its # definition to Student like the following:

Student.List = DefineList.extend({
    "#": Student
});

Note: the typical method used to create a Student is new Student(props). However, if Students have a .connection, map-deep-merge will use Student.connection.hydrateInstance(props). This is useful if Students should be looked up in the connection instanceStore.

For example, Student might have a connection that has an instanceStore, like:

Student.connection = baseMap({
    Map: Student,
    List: Student.List,
    url: "/services/students",
    name: "students"
});

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.14.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news