SlideShare a Scribd company logo
Data Models inData Models in
Angular 1 & 2Angular 1 & 2
Adam Klein
CTO @ 500Tech
UsUs
AngularJS consulting, development, team building
AngularJS-IL Community on meetup.com
helped with ng-conf
Pssst....
AngularJS Course - 20.5
We're looking for experienced
NodeJS developers
AngularJS developers
Data models in AngularData models in Angular
There's no such thing
Angular focuses on VC
You have a service - do whatever
you want with it
?
What's the big deal?What's the big deal?
Unreliable Data
partial
incomplete
stale
Live in a browser
your app keeps restarting
it might be opened in
parallel
it has limited resources
JSON server
sync & async mixed
flakiness
no standard
Angular
bindable
Data models in Angular 1 & 2
Data models in Angular 1 & 2
Data Access != Network AccessData Access != Network Access
Network Layer
routes & parameters
RESTFul APIs
interceptors
http headers
Web Sockets
Data Access Layer (DAL)
data transformation
persisting
caching
access methods
aggregation
Existing solutionsExisting solutions
NetworkNetwork
$http
$resource
RestangularOther
libraries
$resource$resource
1 file, 661 lines of code
built-in to angular
Network:
RESTFul routes
Path & Params building
Interceptors
Data:
Prototypes
Bindable to template
RestangularRestangular
A better version of $resource
5794 stars on github
1 file with 1351 lines of code
Maintained by one Argentian guy
A bit more complex and
configurable
Doesn't matterDoesn't matter
Just wrap it in a serviceJust wrap it in a service
Data AccessData Access
A.K.A.A.K.A.
DALDAL
DAODAO
ModelModel
DataServiceDataService
Breeze
JS-data-angular
Backbone
Model
Your own
Ember Data
Todo MVCTodo MVC
Who owns the data?Who owns the data?
class TodoController {
createTodo(todo) {
todo.completed = false;
this.TodoService.post(todo).then((todo) => {
this.todos.push(todo);
});
}
}
class TodoService {
post(todo) {
return this.$http.post('/todos', todo);
}
}
DAO is in charge ofDAO is in charge of
datadata
Controller is in charge ofController is in charge of
view stateview state
BetterBetter
class TodoController {
createTodo(todo) {
this.saving = true;
this.TodoService.add(todo).then(() => {
this.saving = false;
});
}
}
class TodoService {
add(todo) {
todo.completed = false;
return this.$http.post('/todos', todo)
.then((todo) => {
this.todos.push(todo));
return todo;
});
}
}
OOPOOP
class Todo {
constructor() {
this.completed = false;
}
totalTime() {
return this.completedAt - this.startedAt;
}
}
Working 'offline'Working 'offline'
Don't wait for server
Better UX
when user owns data
editors
Don't allow inputting wrong data
Indications to user
Synchronisation problems
Working offlineWorking offline
class TodoStore {
create(todo) {
this.todos.push(todo);
return $http.post('/todos', todo);
}
}
99 bottles of beer on99 bottles of beer on
the wallthe wall
Bindable to scopeBindable to scope
Controller:
this.beerBottles = DataService.beerBottles;
$interval(() => {
DataService.query();
}, 2000);
DataService:
this.beerBottles = [];
query() {
return this.$http.get('/beer_bottles')
.then((bottles) => this.beerBottles = bottles);
}
Possible solution?Possible solution?
Controller:
this.data = DataService;
$interval(() => {
DataService.query();
}, 2000);
Template:
<div>
{{ Ctrl.data.items.length }} bottles of beer on the wall,<br>
{{ Ctrl.data.items.length }} bottles of beer<br>
if one of the bottles should happen to fall....<br><br>
{{ Ctrl.data.items.length - 1 }} bottles of beer on the wall!
</div>
Don't couple view with DAODon't couple view with DAO
Use angular.copy
constructor() {
this._beerBottlesData = [];
}
query() {
return this.$http('beer_bottles')
.then((bottles) => {
angular.copy(bottles, this._beerBottlesData);
return this._beerBottlesData;
});
}
getList() {
return this._beerBottlesData;
}
Code SmellCode Smell
class DataService {
constructor($state, $modal, $rootScope) {
}
}
CachingCaching
class BeerBottlesService {
query() {
return this.$http('beer_bottles');
}
}
CachingCaching
constructor() {
this._beerBottles = null;
}
query() {
if (this._beerBottles) {
return this._beerBottles;
}
else {
return this.$http('beer_bottles')
.then((bottles) => {
return this._beerBottles = bottles;
});
}
}
CachingCaching
constructor() {
this._beerBottles = null;
}
query() {
if (!this._beerBottles) {
this._beerBottles = this.$http('beer_bottles');
}
return this._beerBottles;
}
Cache the promise, not the data
ParameterisedParameterised
cachingcaching
Maintain a hash of promises
{
1: Promise that object 1 will return
2: Promise that object 2 will return
...
}
Http CacheHttp Cache
Sometimes is good enough
URL based, not resource based
TreesTrees
JSON editorJSON editor
{
config: {
baseUrl: 'http://my.website.com',
port: 3000,
allowedMethods: ['POST', 'GET'],
resources: {
users: {access: 'admin'},
posts: {access: 'user'},
pages: {access: 'guest'}
}
}
}
{
name: 'config',
type: 'Object',
children: [
{
name: 'baseUrl',
type: 'String',
value: 'http://my.website.com'
},
{
name: 'port',
type: 'Integer',
value: 3000
},
{
name: 'allowedMethods',
type: 'Array',
value: ['POST', 'GET', 'DELETE']
},
{
name: 'resources',
type: 'Object',
children: [
...
]
}
]
}
Same data, different representationsSame data, different representations
js-data-angularjs-data-angular
Jason Dobry
js-data - 454 stars
js-data-angular - 932 stars
December 2013
started for angular, inspired by ember-
data
https://www.youtube.com/watch?
v=8wxnnJA9FKw
js-data-angularjs-data-angular
bind to controller
identity maps
query language
sync & async
computed properties
prototyping and static methods
totally configurable
change detection
validations
cache expiration
framework agnostic (even runs on node)
Angular 2.0Angular 2.0
https://www.youtube.com/watch?
v=Bm3eDgZZMFs
https://docs.google.com/document/d/1DM
acL7iwjSMPP0ytZfugpU4v0PWUK0BT6lhya
VEmlBQ/edit
https://docs.google.com/document/d/1US
9h0ORqBltl71TlEU6s76ix8SUnOLE2jabHVg
9xxEA/edit#heading=h.oisbys59gdxa
What's been doneWhat's been done
A lot of research
Collaboration with other teams
GoalsGoals
No Boilerplate
BYOData
Working with existing libraries authors
Don't dictate behaviour
Don't dictate server integration
Recognise different flows
In other words - a fantasy?
Future of webFuture of web
collaborationcollaboration
realtime datarealtime data
offline workoffline work
PhasesPhases
1. Utilities
2. Offline
3. Rich data
More considerationsMore considerations
1. Security
2. Bindability
3. Performance
4. Storage limitations
5. Mocking & Testability
Structured FormsStructured Forms
Bindable realtime dataBindable realtime data
using observables and async pipes
// Component
this.count = http('http://beer.factory/bottles').
map((bottles) => bottles.length)
// Template
<span>
{{ count | async }} bottles of beer on the wall
</span>
Let's finish with aLet's finish with a
watwat
"We want to make API more"We want to make API more
intuitive"intuitive"
How you do http short polling
var beerBottles = Rx.Observable.interval(60 * 2000).
map(() => 'http://beerfactory.com/api/beer_bottles').
flatMapLatest(http).
subscribe()
Data models in Angular 1 & 2
Thank youThank you
Adam Klein
500Tech.com
meetup.com/angularjs-il
hackademy.co.il
github.com/adamkleingit
https://twitter.com/500techil

More Related Content

KEY
Geotalk presentation
PDF
async/await in Swift
PPT
Presentation
PPT
PDF
CouchDB Mobile - From Couch to 5K in 1 Hour
PDF
Using Amazon Simple Db With Rails
PDF
Django Rest Framework and React and Redux, Oh My!
PPT
Enhance Web Performance
Geotalk presentation
async/await in Swift
Presentation
CouchDB Mobile - From Couch to 5K in 1 Hour
Using Amazon Simple Db With Rails
Django Rest Framework and React and Redux, Oh My!
Enhance Web Performance

What's hot (20)

PPTX
ElasticSearch for .NET Developers
ODP
Introducing CouchDB
PDF
HTML5 JavaScript APIs
PPTX
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
ODP
When dynamic becomes static: the next step in web caching techniques
PDF
Future of Web Apps: Google Gears
PDF
Web Crawling with NodeJS
PDF
Ajax Performance Tuning and Best Practices
PDF
jQuery in 15 minutes
PDF
Even faster django
PDF
AJAX Transport Layer
PDF
Hd insight programming
PDF
Supercharging WordPress Development - Wordcamp Brighton 2019
PPTX
SenchaCon 2016: How to Auto Generate a Back-end in Minutes - Per Minborg, Emi...
PPTX
Python Code Camp for Professionals 3/4
PPTX
Python Code Camp for Professionals 4/4
PPT
Frontend Servers and NGINX: What, Where and How
PDF
Url programming
PPT
Spring data iii
ODP
Remove php calls and scale your site like crazy !
ElasticSearch for .NET Developers
Introducing CouchDB
HTML5 JavaScript APIs
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
When dynamic becomes static: the next step in web caching techniques
Future of Web Apps: Google Gears
Web Crawling with NodeJS
Ajax Performance Tuning and Best Practices
jQuery in 15 minutes
Even faster django
AJAX Transport Layer
Hd insight programming
Supercharging WordPress Development - Wordcamp Brighton 2019
SenchaCon 2016: How to Auto Generate a Back-end in Minutes - Per Minborg, Emi...
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 4/4
Frontend Servers and NGINX: What, Where and How
Url programming
Spring data iii
Remove php calls and scale your site like crazy !
Ad

Viewers also liked (20)

PPTX
How to Upgrade Angular 1 to Angular 2 - Piece by Piece
PPTX
Migrating an Application from Angular 1 to Angular 2
PPTX
Angular 2 vs Angular 1
ODP
Introduction to Angular 2
PDF
New World of Angular (v2+)
PPTX
Angular2 / Typescript symposium Versusmind
PPTX
Angular1x and Angular 2 for Beginners
PDF
Space survival game
PPTX
Introduction to Angular JS
PDF
Building single page applications with angular.js
PDF
Embrace the Angular 2 Ethos in Angular 1.x
PDF
Angular2 intro
PDF
Angular JS - Develop Responsive Single Page Application
PPTX
Migrating an application from Angular 1 to Angular 2
PDF
How Angular2 Can Improve Your AngularJS Apps Today!
PDF
Living Things and Non-Living Things
PDF
Angular 2 overview
PPTX
Understanding angular js
PDF
Angular1&2
PPTX
Angular 2 KTS
How to Upgrade Angular 1 to Angular 2 - Piece by Piece
Migrating an Application from Angular 1 to Angular 2
Angular 2 vs Angular 1
Introduction to Angular 2
New World of Angular (v2+)
Angular2 / Typescript symposium Versusmind
Angular1x and Angular 2 for Beginners
Space survival game
Introduction to Angular JS
Building single page applications with angular.js
Embrace the Angular 2 Ethos in Angular 1.x
Angular2 intro
Angular JS - Develop Responsive Single Page Application
Migrating an application from Angular 1 to Angular 2
How Angular2 Can Improve Your AngularJS Apps Today!
Living Things and Non-Living Things
Angular 2 overview
Understanding angular js
Angular1&2
Angular 2 KTS
Ad

Similar to Data models in Angular 1 & 2 (20)

PPTX
Introduction to node.js
PDF
Rethinking Syncing at AltConf 2019
KEY
Writing robust Node.js applications
PPT
AngularJS and SPA
PPTX
Web Technologies - forms and actions
PDF
08 ajax
PDF
Scalable web application architecture
ODP
Web program-peformance-optimization
PDF
SproutCore and the Future of Web Apps
PDF
Introduction to Node.js
PDF
NoSQL meets Microservices - Michael Hackstein
KEY
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
PDF
Play Framework: async I/O with Java and Scala
PPTX
Nodejs.meetup
PDF
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...
PDF
Solutions for bi-directional Integration between Oracle RDMBS & Apache Kafka
ZIP
Javascript Everywhere From Nose To Tail
PDF
Node.js: scalability tips - Azure Dev Community Vijayawada
PDF
Backbone.js — Introduction to client-side JavaScript MVC
KEY
[Coscup 2012] JavascriptMVC
Introduction to node.js
Rethinking Syncing at AltConf 2019
Writing robust Node.js applications
AngularJS and SPA
Web Technologies - forms and actions
08 ajax
Scalable web application architecture
Web program-peformance-optimization
SproutCore and the Future of Web Apps
Introduction to Node.js
NoSQL meets Microservices - Michael Hackstein
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Play Framework: async I/O with Java and Scala
Nodejs.meetup
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...
Solutions for bi-directional Integration between Oracle RDMBS & Apache Kafka
Javascript Everywhere From Nose To Tail
Node.js: scalability tips - Azure Dev Community Vijayawada
Backbone.js — Introduction to client-side JavaScript MVC
[Coscup 2012] JavascriptMVC

More from Adam Klein (10)

PDF
Angular Course - Flux & Redux
PDF
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
PDF
Tales of an open source library
PDF
Es6 everywhere
PDF
Clean up your code
PDF
Ruby for devops
PDF
Lean startups for non-tech entrepreneurs
PDF
Client side unit tests - using jasmine & karma
PDF
Mobile Apps using AngularJS
PDF
3rd party
Angular Course - Flux & Redux
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
Tales of an open source library
Es6 everywhere
Clean up your code
Ruby for devops
Lean startups for non-tech entrepreneurs
Client side unit tests - using jasmine & karma
Mobile Apps using AngularJS
3rd party

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PPTX
Spectroscopy.pptx food analysis technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
A Presentation on Artificial Intelligence
The Rise and Fall of 3GPP – Time for a Sabbatical?
Digital-Transformation-Roadmap-for-Companies.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Spectroscopy.pptx food analysis technology
Spectral efficient network and resource selection model in 5G networks
Advanced methodologies resolving dimensionality complications for autism neur...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Machine learning based COVID-19 study performance prediction
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf

Data models in Angular 1 & 2