[Falcor] Intro to JSON Graph
JSON is a very commonly used data interchange format. Unfortunately while most application domain models are graphs, JSON is designed to model hierarchical information. To get around this problem, Falcor introduces JSON Graph. JSON Graph introduces references to JSON, allowing you to ensure that no object appears more than once in your JSON.
Let say we have a json data to repersent a recently watched list and newly relesase list, in both lists, there is one item is the same:
genreList: [
{
name: 'Recently Watched',
titles: [
{
id: 5221,
name: "House of Cards",
rating: 4.5
}
]
},
{
name: "New Releases",
titles: [
{
id: 5221,
name: "House of Cards",
rating: 4.5
}
]
}
]
Then in recently watched list, I want to give "House of Cards" 5 rating:
model.setValue('genreList[0].titles[0].rating', 5)
.then(function (value) {
model.get('genreList[0..1].titles[0]["name","rating"]')
.then(function (json) {
console.log(JSON.stringify(json, null, 3));
})
});
Here we set the rating as '5', then we print out the list again:
{
"json": {
"genreList": {
"0": {
"titles": {
"0": {
"name": "House of Cards",
"rating": 5
}
}
},
"1": {
"titles": {
"0": {
"name": "House of Cards",
"rating": 4.5
}
}
}
}
}
}
As we can see from the result, the first item is rated as 5, but the second item still renaming 4.5 . But they reperesnt the same item, what we want my rating can affect the item's rating all lists.
But with json, it is hard to do that. That's why we need JSON Graph.
You can convert a json object into a jon graph in two step:
- Each json repersent item should have a unqie id, then you can use the unqie id as key to map object.
- Replace the original data with the json graph ref.
cache: {
titleById: {
5221: {
// id: 5221,
name: "House of Cards",
rating: 4.5
}
},
genreList: [
{
name: 'Recently Watched',
titles: [
{$type: 'ref', value: "titleById[5221]"}
]
},
{
name: "New Releases",
titles: [
{$type: 'ref', value: "titleById[5221]"}
]
}
]
}
5221 is the unqie id to repersent the json object.
Actually there is a helper method can help to refactor the code:
cache: {
titleById: {
5221: {
// id: 5221,
name: "House of Cards",
rating: 4.5
}
},
genreList: [
{
name: 'Recently Watched',
titles: [
$ref("titleById[5221]")
]
},
{
name: "New Releases",
titles: [
$ref("titleById[5221]")
]
}
]
}
----------------------------------------
<!-- index.html -->
<html>
<head>
<!-- Do _not_ rely on this URL in production. Use only during development. -->
<script src="//netflix.github.io/falcor/build/falcor.browser.js"></script>
<script>
var $ref = falcor.Model.ref;
var model = new falcor.Model({
cache: {
titleById: {
5221: {
// id: 5221,
name: "House of Cards",
rating: 4.5
}
},
genreList: [
{
name: 'Recently Watched',
titles: [
$ref("titleById[5221]")
]
},
{
name: "New Releases",
titles: [
$ref("titleById[5221]")
]
}
]
}
}); model.setValue('genreList[0].titles[0].rating', 5)
.then(function (value) {
model.get('genreList[0..1].titles[0]["name","rating"]')
.then(function (json) {
console.log(JSON.stringify(json, null, 3));
})
});
</script>
</head>
<body>
</body>
</html>
[Falcor] Intro to JSON Graph的更多相关文章
- Netflix Falcor获取JSON数据
Netflix开源了JavaScript库Falcor,它为从多个来源获取JSON数据提供了模型和异步机制. Netflix利用Falcor库实现通过JSON数据填充他们网页应用的用户界面.所有来自内 ...
- Falcor 学习一基本使用
falcor 是netflix 公司为了解决自己api数据查询所开发的查询框架,很不错(尽管netflix 也在用graphql )以下是falcor 的 一个简单使用,基于express 框架,使用 ...
- ASP.NET如何使用JSON
关于json,有一个官网:http://www.json.org 上面介绍了每种语言生成json格式的类库,我们只要把他们下载解压之后调用他们其中的组件即可,在.net中我用的是Newtonsoft. ...
- [Falcor] Return the data from server
<!-- index.html --> <html> <head> <!-- Do _not_ rely on this URL in production. ...
- Newtonsoft.Json 序列化踩坑之 IEnumerable
Newtonsoft.Json 序列化踩坑之 IEnumerable Intro Newtonsoft.Json 是 .NET 下最受欢迎 JSON 操作库,使用起来也是非常方便,有时候也可能会不小心 ...
- Newtonsoft.Json 指定某个属性使用特定的时间格式
Newtonsoft.Json 指定某个属性使用特定的时间格式 Intro Newtonsoft.Json 是 .NET 下最受欢迎 JSON 操作库,原为 JSON.Net 后改名为 Newtons ...
- open-falcon监控系统
官方文档 https://book.open-falcon.org/zh/intro/index.html 一.Open-Falcon介绍 1.监控系统,可以从运营级别(基本配置即可),以及应用级别( ...
- 基于谷歌地图的Dijkstra算法水路路径规划
最终效果图如下: 还是图.邻接表,可以模拟出几个对象=>节点.边.路径.三个类分别如下: Node 节点: using System; using System.Collections.Gene ...
- beego中orm关联查询使用解析
这两天在学习beego框架,之前学习的时候遗漏了很多东西,比如orm.缓存.应用监控.模板处理等,这里将通过实例记录下如何使用beego自带的orm进行关联查询操作. 首先说明下,beego的orm有 ...
随机推荐
- 对DNSPOD添加域名解析的一些见解
1.主机记录这步比较简单,输入“www”表示比较常规的域名例如www.abc.com,“@”表示abc.com,“ * ”表示泛解析,匹配所有*.abc.com的域名. 2.记录类型这步,一般常用A记 ...
- Mac下配置node.js 和react-native
最近对JS挺感兴趣的,就琢磨着在mac上配置下环境学习学习,正巧看到了Facebook的react-native,顺便配置了一下. 安装Homebrew 终端输入: ruby -e "$(c ...
- 关于margin-top失效的解决方法
常出现两种情况:(一)margin-top失效 先看下面代码: <div><div class="box1" >float:left</div> ...
- PHP 数据库 ODBC
PHP 数据库 ODBC ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数 ...
- Java学习----运算符与表达式
一.运算符 1.算术运算符 + - * / % ++ -- public class Test7 { public static void main(String[] args) { ...
- jQuery 选择器和JavaScript 选择器的技巧与异常原因
jquery的选择器借鉴了css选择器,核心依然依靠JavaScript的getElementById()和getElementsByTagName()方法,但是他封装了2个方法,让jquery选择器 ...
- POJ3274 hash
POJ3274 问题重述: 已知有n头牛,用一个K位二进制数Ak,Ak-1,...,A1表示一头牛具有的特征,Ai=1表示具有特征i.现给定按顺序排列的N头牛的k位特征值,称某个连续范围内“特征平衡” ...
- postgres常用类型
数值类型 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字节 常用的整数 -2147483648 到 +2147483647 ...
- .net发邮件
// 引入命名空间 using System.Net; using System.Net.Mail; SmtpClient smtp = new SmtpClient(); //实例化一个SmtpCl ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...