$scope
Angular 1.x
ng-controller="myController"
Angular 2.0
document.addEventListener('DOMContentLoaded', function() {
ng.bootstrap(Component);
});
{{modelTest}} - [(ng-model)]="modelTest""In a nutshell, Zone provides what you might consider a 'thread execution' context for JavaScript."
- Jeremy Likness
'this' for changes'$scope.$apply()'Angular 2 site: angular.io
Victor Savkin's Blog: victorsavkin.com
"Google"
"...a Web that is as close to instant as you can get."
- Ars Technica
Syncano uses Channels to poll for real time updates
Channels are a way of providing realtime communication functionality in Syncano. Users can subscribe to Channels in order to get notifications about changes that happen to Data Objects connected to those Channels.- Syncano.io
Everything that Angular displays on the front end is being polled from Syncano on the backend in real time.
Follow along: http://bit.ly/21ygVcT
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/2.0.0-alpha.44/angular2.sfx.dev.js"></script>
<script src="syncano.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<h1>Realtime Item List</h1>
<cmp></cmp>
</body>
</html>
var Cmp = ng.
Component({
selector: 'cmp',
template:
'<ul style="width:15em">' +
'<li *ng-for="#item of list" id="{{item.id}}">' +
'{{ item.name }} <button (click)="removeItem(item)" style="float:right;">X</button>' +
'</li>' +
'</ul>' +
'<input id="textEntry" #textbox (keyup)="doneTyping($event)">' +
'<button id="submitButton" (click)="addItem(textbox.value)">Add Item</button>',
directives: [ng.NgModel, ng.FORM_DIRECTIVES, ng.NgFor]
})
.Class({
constructor: [function Cmp() {
var self = this; // ES5
this.list = []; // list of items
...
}]
});
document.addEventListener('DOMContentLoaded', function() {
ng.bootstrap(Cmp);
});
// Syncano variables
var sync = new Syncano({accountKey:myAccountKey}); // for creating obj
var instance = new Syncano({apiKey:myApiKey, instance:myInstance}); // for real time
var realtime = instance.channel(myChannel).watch(); // real time instance variable
// Initial List from Syncano
sync.instance(myInstance).class(myClass).dataobject().list()
.then(function(res){
for(i=0;i<res.objects.length;i++){
self.list.push({ // push to array
name: res.objects[i].name,
id: res.objects[i].id
});
}
})
.catch(function(err){
console.log(err);
});
// Realtime Event Listeners - (scroll down)
realtime.on('create', function(data) {
self.list.push({ // push new item to array with Syncano data
name: data.name,
id: data.id
});
});
realtime.on('delete', function(data) {
for(var i = self.list.length - 1; i >= 0; i--) {
if(self.list[i].id === data.id) {
self.list.splice(i, 1); // remove from array
}
}
});
realtime.on('error', function(data) {
console.log(data);
});
this.addItem = function(item) { // add item to Syncano
var newItem = {
"name":item,
"channel":"itemlist"
};
sync.instance(myInstance).class(myClass).dataobject().add(newItem)
.then(function(res){
console.log(res);
})
.catch(function(err){
console.log(err);
})
};
this.removeItem = function(item){ // remove item from Syncano
sync.instance(myInstance).class(myClass).dataobject(item.id).delete()
.then(function(res){
console.log("Item: [" + item.name + "] was deleted");
})
.catch(function(err){
console.log(err);
});
};
this.doneTyping = function($event) { // watches for keys when done typing
if($event.which === 13) { // 'enter' key
this.addItem($event.target.value);
$event.target.value = null;
}
};
We're in a world of real time web. As Angular moves towards using execution contexts to provide our apps with real time capabilities (even though some calls are asynchronous), we should focus on building our apps and web services for this real time web.
Developer Evangelist for Syncano
Live Slides: http://bit.ly/1OzGZ23
Github Slide Repo: http://bit.ly/1SzcRmp