0

In Javascript, strings are immutable, and making references to strings is equivalent to copying the string istelf.

In Falcor, I can make references to strings using {$type:"ref", value:[...]}. Falcor claims to maintain data consistency, so that if the model contains references, they end up pointing to the same JS object (so maintaining consistency becomes trivial).

From Falcor docs on JSON Graph:
JSON Graph allows a graph to be modeled as JSON without introducing duplicates. Instead of inserting an entity into the same message multiple times, each entity with a unique identifier is inserted into a single, globally unique location in the JSON Graph object.

However, I can't see how this could apply to strings.

Let say, I have a model like this:

{jsonGraph:{
    foo: {text: 'aaa'},
    bar: {text: {$type: "ref", value: ["foo", "text"]}},
}}

What happens when I update foo.text? Will bar.text be updated too? And if yes, how does it work?

Community
  • 1
  • 1
mik01aj
  • 11,928
  • 15
  • 76
  • 119

1 Answers1

1

What happens when I update foo.text? Will bar.text be updated too?

If you update foo.text and then get('bar.text') you'll get whatever the current value of foo.text is. It's as if the value got updated in both places, if you want to think of it that way.

But really there is no bar.text value, it's just a reference, and if the thing being referenced changes, then things that follow that reference will encounter that new value.

Think of it like this:

var text = '123';
function getText() {
  return text;
}

getText() behaves like a reference to text. If you set text = 456 then getText() will start returning a different value, even if getText() didn't itself change.

greim
  • 9,149
  • 6
  • 34
  • 35