javascript - Can AngularJS auto-update a view if a persistent model (server database) is changed by an external app? -
i'm starting familiarize angularjs, build web app has view gets auto-upated in real-time (no refresh) user when changes in server-side database.
can angularjs handle (mostly) automatically me? , if so, basic mechanism @ work?
for example, somehow setup angularjs poll db regularly "model" changes? or use sort of comet-like mechanism notify angularjs client-side code model has changed?
in application, challenge other (non-web) server-side software updating database @ times. question applies equally pure web-apps might have multiple clients changing database through angularjs web clients, , each need updated when 1 of them makes change db (model).
you have few choices...
you polling every x milliseconds using
$timeout
,$http
, or if data you're using hooked rest service, use$resource
instead of$http
.you create service uses websocket implementation , uses
scope.$apply
handle changes pushed socket. here's example using socket.io, node.js websocket library:myapp.factory('socket', function($rootscope) { var socket = io.connect('http://localhost:3000'); //override socket.on $apply changes angular return { on: function(eventname, fn) { socket.on(eventname, function(data) { $rootscope.$apply(function() { fn(data); }); }); }, emit: socket.emit }; }) function myctrl($scope, socket) { socket.on('content:changed', function(data) { $scope.data = data; }); $scope.submitcontent = function() { socket.emit('content:changed', $scope.data); }; }
you high tech , create websocket implementation syncs angular model server. when client changes something, change gets automatically sent server. or if server changes, gets sent client.
here's example of in old version of angular, again using socket.io: https://github.com/mhevery/angular-node-socketio
edit: #3, i've been using firebase this.
Comments
Post a Comment