1

I have a cache service, i would like to store data in it.

angular.module('core').service('markerCache', function(){
    var cache = [];
    this.set_cache = function(arr){
        cache = arr;
        this.cached = true;
    };
    this.cached = false; //this must be read-only and must be property, not function
});

And i also would like to define read-only(value can be set only inside service) property in angular services(factory, service) if possible, if not - any workaround would be awesome.
There is a way to define read-only properties in javascript, but how to do it in angular way?
By read-only i mean setting value inside service. For example

angular.module('core').controller('Ctrl', function($scope, markerCache){
    markerCache.cached = false; //disable setter outside of markerCache service
});

Update, for those who is still interested, here's working service

angular.module('core').service('markerCache', function(){
    var cache = [];
    var cached = false;
    Object.defineProperty(this, "cache", {
        get: function() {
            return cache;
        },
        set: function(val){
            cache = val;
            cached = true;
        }
    });
    Object.defineProperty(this, "cached", {
        get: function() {
            return cached;
        },
        set: angular.noop
    });
});
Community
  • 1
  • 1
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69

3 Answers3

4

You can use closure to hide a variable and expose a property using Object.defineProperty to read the variable.

angular.module('core').service('markerCache', function(){
    var cache = [];
    var cached = false; // private variable
    this.set_cache = function(arr){
        cache = arr;
        cached = true;
    };
    Object.defineProperty(this, "cached", { // public readonly getter
      get: function() {
        return cached;
      },
      set: function(val) {
        //throw new Error('Cannot set internal cache state'); //throw custom exception
      }
      //set: angular.noop //or do nothing, empty setter
    })
});
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
0

Are you looking for AngularJS constants?

constant(name, value); Register a constant service, such as a string, a number, an array, an object or a function, with the $injector. Unlike value it can be injected into a module configuration function (see angular.Module) and it cannot be overridden by an Angular decorator.

Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70
0

The variable should not be associated with this context, you could make it private by making it var cached, also defining getter inside your service for getting value of cache so that consumer will access that value by getter function.

angular.module('core').service('markerCache', function(){
    var cache = [], 
        cached = false; //made privet
    this.set_cache = function(arr){
        cache = arr;
        cached = true;
    };
    this.getCache = function(){
        return cached;
    }
});
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299