简体   繁体   English

Angular.js:如何避免缓存服务?

[英]Angular.js: How can I avoid caching services?

I have a service that's connecting my clients to the Socket.io server : 我有一项将客户端连接到Socket.io服务器的服务:

var serviceSocketIo = angular.module('myApp.services', []);

serviceSocketIo.factory('mySocket',['$rootScope', '$routeParams', function ($rootScope, $routeParams) {
  var base = $rootScope.base, //base = 'http://localhost:3000'
  channel = '/' + $routeParams.game,
  url = base + channel,
  mySocket;

  console.log('service: connection to ' + url);
  mySocket = io.connect(url);
  return {
    base: base,
    channel: channel,
    socket: mySocket,
    on: function (eventName, callback) {
      mySocket.on(eventName, function () {  
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(mySocket, args);
        });
      });
    },
    emit: function (eventName, data, callback) {
      mySocket.emit(eventName, data, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          if (callback) {
            callback.apply(mySocket, args);
          }
        });
      })
    }
  };
}]);

Please note that the namespace depends on the $routePrams paramaters. 请注意,名称空间取决于$ routePrams参数。

What I need is to delete the service from cache before instanciate it. 我需要的是在实例化之前从缓存中删除该服务。 So I'll be sure the services will connect clients to the right url. 因此,我将确保服务将客户端连接到正确的URL。 Or if there is an other way to force my service to check the parameters ? 还是有其他方法强制我的服务检查参数?

Factories and Services in angular are singletons and there is no configuration to make them otherwise. 角度的工厂和服务为单例,没有其他配置。 If you want such control you need to use methods on the $provide object: 如果要进行此类控制,则需要在$ provide对象上使用方法:

Get access to provide using app.config: 使用app.config获取提供服务的权限:

app.config(function($provide) {
   // Now you have provide 
});

You can use it to create your own custom instantiation policy using what you return from $get: 您可以使用它使用从$ get返回的内容来创建自己的自定义实例化策略:

$provide.provider('foo', function(){
  this.$get = function(dep) {...}
});

Reference: http://slides.wesalvaro.com/20121113/#/2/6 参考: http//slides.wesalvaro.com/20121113/#/2/6

What's letting you from creating a service that creates the socket for you? 是什么让您无法创建可为您创建套接字的服务?

var services = angular.module('myApp.services', []);

services.service('SocketService', function () {
    var SocketService = function () {
        this.createSocket = function (url) {
            return io.connect(url);
        }
    };

    return SocketService;
});

The services / factory methods are run only once and from that point on the return value is used. 服务/工厂方法仅运行一次,并从那时起使用返回值。 If you want to customize the capabilities of what you return, put these as arguments in the function members of what you return. 如果要自定义返回值的功能,请将它们作为参数放入返回值的函数成员中。 eg notice yourArg: 例如,注意yourArg:

var serviceSocketIo = angular.module('myApp.services', []);

serviceSocketIo.factory('mySocket',['$rootScope', '$routeParams', function ($rootScope, $routeParams) {
  var base = $rootScope.base, //base = 'http://localhost:3000'
  channel = '/' + $routeParams.game,
  url = base + channel,
  mySocket;

  console.log('service: connection to ' + url);
  mySocket = io.connect(url);
  return {
    base: base,
    channel: channel,
    socket: mySocket,
    // Notice yourArg 
    on: function (eventName, callback, yourArg) {
      // Use yourArg to customize behaviour: 
      mySocket.on(eventName, function () {  
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(mySocket, args);
        });
      });
    },
    emit: function (eventName, data, callback) {
      mySocket.emit(eventName, data, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          if (callback) {
            callback.apply(mySocket, args);
          }
        });
      })
    }
  };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM