[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有 ...
随机推荐
- CSS 布局Float 【2】
1.页面标准文档流.浮动层.float属性 1.1 文档流 HTML页面的标准文档流(默认布局)是:从上到下,从左到右,遇块(块级元素)换行. 1.2 浮动层 浮动层:给元素的float属性赋值后,就 ...
- [JS] save txt file
(function () { var blob = new Blob(['content'], {type: 'text/plain; charset=utf-8'}), blobUrl = URL. ...
- c++面试(二)
1.宏参数的连接 #define CONS(a,b) (int)(a##e##b) CONS(2,3) =>2e3 =2000 2.const int b=10; int c=20; const ...
- mysql的limit经典用法及优化
用法一 SELECT `keyword_rank`.* FROM `keyword_rank` WHERE (advertiserid='59') LIMIT 2 OFFSET 1; 比如这个 ...
- Bootstrap中的 Typeahead 组件
Bootstrap 中的 Typeahead 组件其实就是嵌入到其中的typeahead.js插件,可以完成输入框的自动匹配功能,在通过一些人工的调整基本可以胜任所有的匹配功能和场景,下面介绍下简单的 ...
- [jQuery] $.grep使用
1.$.grep的功能是查找过滤功能的数组,原数组不受影响. 2.参数定义 jQuery.grep( array, function(elementOfArray, indexInArray), [ ...
- Testlink接口使用方法-python语言远程调用
deepin@deepin-pc:~/test$ cat libclienttestlink.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- #! ...
- 驱动读写进程内存R3,R0通信
stdafx.h 头文件代码 #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. #defin ...
- HDU-2054 A==B?
#include<stdio.h>#include<string.h>char n[100000], m[100000];int main(){ int i, j, len_n ...
- PHP之路——PHPExcel使用
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGMAAAJkCAIAAAA6GnvRAAAgAElEQVR4nOzd918bV/ov8Pv33Y2RNC