Medet Tleukabiluly
Medet Tleukabiluly

Reputation: 11930

Readonly property in service

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
    });
});

Upvotes: 1

Views: 1250

Answers (3)

Bryan Chen
Bryan Chen

Reputation: 46598

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
    })
});

Upvotes: 4

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

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;
    }
});

Upvotes: 0

Praveen Prasannan
Praveen Prasannan

Reputation: 7123

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.

Upvotes: 0

Related Questions