Skip to content

Instanced nodes

By default, every time a MapViewerNode is emited, a new plain object is generated, thus:

  • If you get the same node multiple times, the javascript instances are not the same
  • If you update the node, old instances of the objet DO NOT update.

You can change the default behavior when loading the module with the instanced_nodes flag. with this flag set to true:

  • Emitted instances of a particular MapViewerNode will always be the same instance, improving performance.
  • The instance data will be updated
  • If the map is reset, the instance will be unusable.
  • Node properties are getters (data is retrieved only when asked), improving performance.

To get snapshots of a node, you can use the toJSON method.

1
2
3
4
DVM.loadModule("map_viewer", { 
    container: "container", 
    instanced_nodes: true 
}).then(...);
// instanced_nodes = false

const n1 = viewer.getNodeById("S_100");
const n2 = viewer.getNodeById("S_100");

console.log(n1 === n2); // false

const before = viewer.getNodeById("S_123");
console.log(before.state); // unavailable

viewer.setAvailable("section", "S_123");
const after = viewer.getNodeById("S_123");

console.log(before.state) // unavailable
console.log(after.state) // available
// instanced_nodes = true

const n1 = viewer.getNodeById("S_100");
const n2 = viewer.getNodeById("S_100");
const json = n1.toJSON();

console.log(n1 === n2); // true
console.log(n1 === json); // false

const before = viewer.getNodeById("S_123");
const before_json = before.toJSON();
console.log(before.state); // unavailable

viewer.setAvailable("section", "S_123");
const after = viewer.getNodeById("S_123");

console.log(before.state) // available
console.log(after.state) // available
console.log(before_json.state) // unavailable