8. State of artState of art
JSON is not perfectJSON is not perfect
/users/1
{
id: "1",
name: "Mathieu Breton",
job: "CTO",
company: {
id: "2"
name: "JsRepublic",
address : "11 ..."
}
}
9. State of artState of art
JSON is not perfectJSON is not perfect
/users/1
{
id: "1",
name: "Mathieu Breton",
job: "CTO",
company: {
id: "2"
name: "JsRepublic",
address : "11 ..."
}
}
/companies/2
{
id: "2"
name: "JsRepublic",
address: "11 ..."
}
10. State of artState of art
JSON is not perfectJSON is not perfect
/users/1
{
id: "1",
name: "Mathieu Breton",
job: "CTO",
company: {
id: "2"
name: "JsRepublic",
address : "11 ..."
}
}
/companies/2
{
id: "2"
name: "JsRepublic",
address: "11 ..."
}
11. So why ?So why ?
Find a solution to get the data from a backend, with an
uncoupled, lightweight and easy to cache architecture.
And ... Have a model more closed to the reality. In other
word, a graph.
12. WhatWhat
Falcor is a library which presents the data from backend
like a JSON Graph in the Front side. It deals for us the
loading, caching, bashing.
And give a way to define routes to serialize on demand.
Version : 0.1.17
Creation Date : April 2015
“The data is the API
22. DataSource APIDataSource API
Call
datasource.
call(
// the callPath
["todos", "remove"],
// the args array containing the id
// of the todo to remove
[42],
[],
// retrieve the length of the list after the function has completed
[
["length"]
]).
subscribe((jsonGraphEnvelope) => console.log(jsonGraphEnvelope));
23. Router - DataSourceRouter - DataSource
Used in server side, it maps path to data
const dataSource = new Router([
{
route: 'todos.push',
call(callPath, args) {
const todoId = args[0];
return todosService.removeTodos(todosId)
.then((todoIdAndLength) => [
{
path: ['todos', {from: todoId, to: todoIdAndLength.length}],
invalidated: true
},
{
path: ['todos', 'length'],
value: titleIdAndLength.length
}
]);
}
}
]);
25. Model exampleModel example
const model = new falcor.Model({
source: new falcor.HttpDataSource('/model.json')
});
model.getValue('todos[0].name').then(console.log);
27. Simple client side exampleSimple client side example
const model = new falcor.Model({
cache: {
user: {
name: "Frank",
surname: "Underwood",
address: "1600 Pennsylvania Avenue, Washington, DC"
}
}
});
// prints "Underwood" eventually
model.getValue("user.surname").
then(console.log);
28. Simple client side exampleSimple client side example
const model = new falcor.Model({
cache: {
user: {
name: "Frank",
surname: "Underwood",
address: "1600 Pennsylvania Avenue, Washington, DC"
}
}
});
// prints "Underwood" eventually
model.getValue("user.surname").
then(console.log);
Promise
Async
Node callback style
Node stream
29. Simple client side exampleSimple client side example
const model = new falcor.Model({
source: new falcor.HttpDataSource("/model.json")
});
// prints "Underwood" eventually
model.getValue("user.surname").
then(console.log);
(with loading)
30.
31. Over the networkOver the network
/model.json?paths=["user.surname"]
GET /model.json?paths=["user.surname"]
{
user: {
surname: "Underwood"
}
}
32. Server side simple exampleServer side simple example
// app.js
const express = require('express');
const falcorMiddleware = require('falcor-express');
const TODORouter = require('./todo-router');
const app = express();
// Create a new Router instance for each new request
app.use('/model.json', falcorMiddleware.dataSourceRoute((req, res) =>
new TODORouter;
));
const server = app.listen(80);
33. Server side simple exampleServer side simple example
// todo-router.js
const router = new Router([
{
route: "user['name','surname','address']",
get(pathSet) {
return userService.loadUser().then((user) =>
pathSet[1].map((userKey) => ({
path: ["user", userKey],
value: user[userKey]
}))
);
}
}
]);