javascript - What problems would I run into if I make all Data available to all Controllers? -


my angularjs crud application processes it's information on websocket server. (this updates 1 user automatically pushed users without need massive http polling)

i realized on have set services differently http services. normally, each model working with, give them own service populate particular model. however, not feasible websocket connection, because don't want separate connection each service. therefore, there couple of solutions.

1) set single service establishes connection, share connection other services use service make specific queries

2) make single, type-agnostic service used controllers need access connection , data.

option 2 seemed easier manage , reusable across applications, started on that. when realized opportunity. rather explicitly creating models each type of data client receive, create master data object, , dynamically create child objects of myservice.data needed when data flows in requests. thus, if ever need update model, update model @ server level, , client knows how receive it; need controller knows how use it.

however, opportunity brings drawback. apparently, because myservice.data empty, childless object @ creation, scope wants reference future children have simple reference object itself.

for example, $scope.user = myservice.data.user throws error, because object doesn't exist @ declaration. appear option each controller have $scope.data = myservice.data, , view each controller have use < ng-model='data'>, declarations being {{data.user.username}}. have tested it, , work.

my question this; there way can best of both worlds? can have service update it's data model dynamically, yet still have controllers access part need? i? feeling quite clever until realized of controllers going have access entire data model... can't decide if huge problem.

here service:

app.factory('websocketservice', ['$rootscope', function ($rootscope) {     var factory = {         socket: null,         data: {},         startconnection: function () {             //initialize websocket             socket = new websocket('ws://localhost:2012/')             socket.onopen = function () {               //todo: need happen onopen?             }             socket.onclose = function () {               //todo: need happen onclose?             }             socket.onmessage = function (event) {                 var packet = json.parse(event.data);                 ////model of packet:                 ////packet.data: serialised object contains needed data                 ////packet.operation: data                 ////packet.model: child object of factory.data use                 ////packet.property: used update , delete find specific object property who's name matches string, , who's value matches packet.data                 //deserialize data                 packet.data = json.parse(packet.data);                 //"refresh" used reload array                  // of objects being stored in factory.data[packet.model]                 // used getall commands , manual user refreshes                 if (packet.operation == "refresh") {                     factory.data[packet.model] = packet.data                 }                 //push used add object existing array of objects.                   //the server send after sends successful post command websocket server                 if (packet.operation == "push") {                     factory.data[packet.model].push(packet.data)                 }                 if (packet.operation == "splice") {                     (var = 0; < factory.data[packet.model].length; i++) {                         (var j = 0; j < packet.data.length; j++){                             if (factory.data[packet.model][i][packet.property] == packet.data[j][packet.property]) {                                 factory.data[packet.model].splice(i, 1);                                 i--;                             }                         }                     }                           }                 // used update existing objects within array.  packet.data array, although in cases have 1 value.                   if (packet.operation == "update") {                     (var = 0; < factory.data[packet.model].length; i++) {                         (var j = 0; j < packet.data.length; j++) {                             if (factory.data[packet.model][i][packet.property] == packet.data[j][packet.property]) {                                 factory.data[packet.model][i] = packet.data[j]                                 i--;                             }                         }                     }                 }                 //sent websocket server after has authenticated user, sending user information has found.                   if (packet.operation == "authentication") {                     if (packet.data == null) {                         //todo: authentication failed.  alert user somehow                     }                     else {                         factory.data.user = packet.data;                         factory.data.isauthenticated = true;                     }                  }                 $rootscope.$digest();             }           },         stopconnection: function () {             if (socket) {                 socket.close();             }         },         //sends serialised command websocket server according it's api.         //the dataobject must serialised string before can placed packet object,which serialised.           //this because backend framework c#, must see controller , operation use before knows how deserialise dataobject.         sendpacket: function (controller, operation, dataobject) {             if (typeof controller == "string" && typeof operation == "string") {                 var data = json.stringify(dataobject);                 var packet = { controller: controller, operation: operation, data: data };                 var packetstring = json.stringify(packet);                 socket.send(packetstring);             }         }     }     return factory }]); 

here simple controller accesses user information. used in permanent header <div> in index.html, outside of dynamic <ng-view>. responsible firing websocket connection.

app.controller("authenticationcontroller", function ($scope, websocketservice) {     init();      function init() {         websocketservice.startconnection();     }     //this way have found access service data.     //$scope.user = websocketservice.data.user doesn't work     //$scope.user = $scope.data.user doesn't work     $scope.data = websocketservice.data }); 

and here html uses controller

    <div data-ng-controller="authenticationcontroller">         <span data-ng-model="data">{{data.user.username}}</span>     </div> 

one thing store data object on root scope, , set watches on various controllers watch whatever controller-specific keys need:

// modules `run` function called once  // injector finished loading modules. app.run(function($rootscope, websocketservice) {   websocketservice.startconnection();   $rootscope.socketdata = websocketservice.data; });  // set $watch in controller app.controller("authenticationcontroller", function($scope) {   $scope.$watch('socketdata.user', function(newuser, olduser) {     // assign user when becomes available.     $scope.user = newuser;   }); }); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -