[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有 ...
随机推荐
- 使用ol,添加图书销售排行榜
如果想在网页中展示有前后顺序的信息列表,怎么办呢?如,当当网上的书籍热卖排行榜,如下图所示. 这类信息展示就可以使用<ol>标签来制作有序列表来展示. 语法: <ol> < ...
- nodejs概论(实操篇)
什么是模块? 模块分为原生模块(node.jsAPI提供的原生模块,在启动时已经被加载)和 文件模块(动态加载模块,主要由原生模块module来实现和完成.通过调 用node.js的require方法 ...
- idea sass scss配置
1.安装Ruby win 直接http://rj.baidu.com/soft/detail/22711.html?ald mac linux https://ruby.taobao.org/ 可下 ...
- c/c++内存机制(一)(转)
转自:http://www.cnblogs.com/ComputerG/archive/2012/02/01/2334898.html 一:C语言中的内存机制 在C语言中,内存主要分为如下5个存储区: ...
- excel设置单元格不可编辑
把允许编辑的单元格选定,右键-设置单元格格式-保护,把锁定前的对钩去掉.再点工具-保护工作表.这样就可以只让你刚才设定的单元格允许编辑,其他不允许.
- ThinkInJava4读书笔记之第七章隐藏实施过程
第7章 多形性 上溯造型:将一个对象作为它自己的类型使用,或者作为它的基础类型的一个对象使用.取得一个对象句柄,并将其作为基础类型句柄使用. 方法调用的绑定:将一个方法调用同一个方法主体连接到一起就称 ...
- jquery easy ui 学习 (8)basic treegrid
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python里的拷贝=====》很容易错误的
不能直接用 = 复制: import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy.copy(a) #对象拷贝,浅 ...
- 浅谈iOS视频播放的N种解决方案
简 注册登录 添加关注 作者 Maru2016.03.22 20:46* 写了4349字,被135人关注,获得了207个喜欢 字数1621 阅读2895 评论43 喜欢159 header ...
- codevs 1107 等价表达式
传送门 题解:第一眼这题好像非常难得样子,简直没有思路.但是这可以用栈带入特殊值来解决.这里用到两个栈,一个是存贮数字,另一个存贮运算符,按优先级进行运算.当读入的运算符比运算符栈的栈顶元素优先级低时 ...