On Github devintyler / real-time-angular2
Angular 1.x
ng-controller="myController"
Angular 2.0
<my-component></my-component>
document.addEventListener('DOMContentLoaded', function() { ng.bootstrap(Component); });Now, since components are being used, the bootstrapping is called in your app file with the components being used as the parameters. The important thing is that you don't bootstrap until after DOMContentLoaded.
- Jeremy Likness
- quote by Jeremy - basically, Zone.js allows you to run sync and async functions, and have wrap them all in a zone or "execution context" - I like to think of it as a step between the global context and the function context, depending on where the zone isAngular 2 site: angular.io
Victor Savkin's Blog: victorsavkin.com
"Google"
- 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.ioEverything 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] })- selector: selects DOM element to inject component - template: HTML template - *ng-for different - #name is identifier for DOM element - directives: standard Angular 2 directives
.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.
There are many data solutions out there, whether you build your own, or use one like Syncano provides that's up to you. I'm just excited that Angular is now providing us with a simple way to keep our back-end in sync in real time with our front-end, and we didn't have to touch the scope to do it.Developer Evangelist for Syncano
Live Slides: http://bit.ly/1OzGZ23
Github Slide Repo: http://bit.ly/1SzcRmp