javascript - 如果我让所有数据对所有 Controller 可用,我会遇到什么问题?

标签 javascript angularjs angularjs-service angularjs-controller

我的 AngularJS CRUD 应用程序通过 WebSocket 服务器处理它的信息。 (这主要是为了将一个用户的更新自动推送给所有用户,而不需要大量的 HTTP 轮询)

我很早就意识到我必须以不同于通常使用 HTTP 服务的方式来设置我的服务。通常,对于我正在使用的每个模型,我都会为它们提供自己的服务来填充该特定模型。但是,这对于 Websocket 连接是不可行的,因为我不想为每个服务建立单独的连接。因此,有几个解决方案。

1) 设置一个建立连接的服务,然后与将使用该服务进行特定查询的其他服务共享该连接

2) 制作一个单一的、类型不可知的服务,所有需要访问连接和数据的 Controller 都将使用该服务。

选项 2 似乎更易于管理,并且可以跨应用程序重用,所以我开始使用它。那时我才意识到这实际上是一个机会。我可以创建一个主控 data,而不是为客户端可以接收的每种类型的数据显式创建模型。对象,并动态创建 myService.data 的子对象当数据从请求流入时根据需要。因此,如果我需要更新我的模型,我只需在服务器级别更新模型,客户端已经知道如何接收它;它只需要一个知道如何使用它的 Controller 。

但是,这个机会带来了一个缺点。显然,因为 myService.Data在创建时是一个空的、没有子对象的对象,任何想要引用其 future 子对象的作用域都必须简单地引用对象本身。

例如,$scope.user = myService.data.user抛出错误,因为该对象在声明时不存在。看来我唯一的选择是让每个 Controller 只拥有 $scope.data = myservice.data ,每个 Controller 的 View 只需使用 < ng-model='data'> , 声明类似于 {{data.user.username}} .我已经测试过了,这确实有效。

我的问题是这样的;有什么办法可以两全其美吗?我可以让我的服务动态更新它的数据模型,但仍然让我的 Controller 只访问他们需要的部分吗?我?在我意识到我所有的 Controller 都可以访问整个数据模型之前,我感觉自己很聪明……但老实说,我无法确定这是否是一个大问题。

这是我的服务:

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: Does anything need to happen OnOpen?
            }
            socket.onclose = function () {
              //todo: Does anything need to happen OnClose?
            }
            socket.onmessage = function (event) {
                var packet = JSON.parse(event.data);
                ////Model of Packet:
                ////packet.Data: A serialised Object that contains the needed data
                ////packet.Operation: What to do with the Data
                ////packet.Model: which child object of Factory.data to use
                ////packet.Property: used by Update and Delete to find a specific object with a property who's name matches this string, and who's value matches Packet.data
                //Deserialize Data
                packet.Data = JSON.parse(packet.Data);
                //"Refresh" is used to completely reload the array 
                // of objects being stored in factory.data[packet.Model]
                // Used for GetAll commands and manual user refreshes
                if (packet.Operation == "Refresh") {
                    factory.data[packet.Model] = packet.Data
                }
                //Push is used to Add an object to an existing array of objects.  
                //The server will send this after somebody sends a successful POST command to the WebSocket Server
                if (packet.Operation == "Push") {
                    factory.data[packet.Model].push(packet.Data)
                }
                if (packet.Operation == "Splice") {
                    for (var i = 0; i < factory.data[packet.Model].length; i++) {
                        for (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 to update existing objects within the Array.  Packet.Data will be an array, although in most cases it will usually only have one value.  
                if (packet.Operation == "Update") {
                    for (var i = 0; i < factory.data[packet.Model].length; i++) {
                        for (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 by WebSocket Server after it has properly authenticated the user, sending the user information that it 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 a serialised command to the Websocket Server according to it's API.
        //The DataObject must be serialised as a string before it can be placed into Packet object,which will also be serialised.  
        //This is because the Backend Framework is C#, which must see what Controller and Operation to use before it knows how to properly Deserialise the 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
}]);

这是一个访问用户信息的简单 Controller 。它实际上用于永久 header 中 <div>在 Index.html 中,在动态之外 <ng-view> .它负责启动 Websocket 连接。

App.controller("AuthenticationController", function ($scope, WebSocketService) {
    init();

    function init() {
        WebSocketService.startConnection();
    }
    //this is the ONLY way that I have found to access the Service Data.
    //$scope.user = WebSocketService.data.user doesn't work
    //$scope.user = $scope.data.user doesn't even work
    $scope.data = WebSocketService.data
});

这是使用该 Controller 的 HTML

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

最佳答案

您可以做的一件事是将 data 对象存储在根作用域中,并在您的各种 Controller 上设置监视以监视它们需要的任何特定于 Controller 的键:

// The modules `run` function is called once the 
// injector is finished loading all its modules.
App.run(function($rootScope, WebSocketService) {
  WebSocketService.startConnection();
  $rootScope.socketData = WebSocketService.data;
});

// Set up a $watch in your controller
App.controller("AuthenticationController", function($scope) {
  $scope.$watch('socketData.user', function(newUser, oldUser) {
    // Assign the user when it becomes available.
    $scope.user = newUser;
  });
});

关于javascript - 如果我让所有数据对所有 Controller 可用,我会遇到什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16672570/

相关文章:

javascript - 如何从嵌套的 ng-repeat 中调用多个 angularjs 服务调用

javascript - AngularJS:打开一个新的浏览器窗口,但仍然保留范围和 Controller 以及服务

javascript - Javascript 中 ASP.NET View 状态的大小

javascript - 将变量从 Javascript 传递到 Flask,然后加载模板

angularjs - 刷新时的 Angular UI 路由器动态状态变为 404

javascript - 从 Cordova MediaFire 上传护理文件

javascript - 谷歌图表获取自定义工具提示来处理动画

javascript - 什么取代了 "with"?

javascript - value() 或 constant() 可以在 AngularJS 中相互使用吗?

angularjs - 打开新窗口angularjs后在 Controller 之间共享变量