javascript - Falcor 模型如何处理对字符串的引用?

标签 javascript reference data-consistency falcor

在 JavaScript 中,strings are immutable ,引用字符串相当于复制字符串 istelf。

在 Falcor 中,我可以使用 {$type:"ref", value:[...]} 来引用字符串。 Falcor 声称要保持数据一致性,因此如果模型包含引用,它们最终会指向同一个 JS 对象(因此保持一致性变得微不足道)。

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.

但是,我不明白这如何应用于字符串。

假设我有一个这样的模型:

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

更新 foo.text 时会发生什么? bar.text 也会更新吗?如果是,它是如何工作的?

最佳答案

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

如果您先更新 foo.text,然后更新 get('bar.text'),您将获得 foo.text 的当前值。如果您想这样想的话,就好像这两个地方的值都更新了。

但实际上没有 bar.text 值,它只是一个引用,如果引用的事物发生变化,那么该引用后面的事物将遇到新值。

这样想:

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

getText() 的行为类似于对 text 的引用。如果您设置了 text = 456,那么 getText() 将开始返回不同的值,即使 getText() 本身没有改变。

关于javascript - Falcor 模型如何处理对字符串的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34988074/

相关文章:

javascript - 向管道 Angular 添加一个字符

iPhone 如何使用 ARC 正确处理 Core Foundation 引用?

caching - 高并发环境下NoSQL+分布式缓存的数据一致性

javascript - 为什么这个 javascript 自动填充多个字段不起作用?

JavaScript : Escaping single quote in function parameter doesn't work

javascript - ES2015 的 React 数组映射问题

c++ - 与 const 引用关联的临时对象的生命周期(方法链接)

c++ - 在 C++ 中安全地传递对抽象类的引用

database - 数据创建响应超时的最佳重试策略

c - 如何在嵌入式 C 代码库中查找与数据一致性相关的问题?