I am in need of a cache service to store data efficiently.
angular.module('core').service('markerCache', function(){
var cache = [];
this.set_cache = function(arr){
cache = arr;
this.cached = true;
};
this.cached = false; //this should be a read-only property, not a function
});
Additionally, I am looking to create read-only properties in Angular services (factory, service). If this is not directly possible, any workaround suggestions would be appreciated.
There is a way to define read-only properties in JavaScript, but I am curious about an Angular-specific approach.
By read-only, I mean the ability to set values inside the service only, for example:
angular.module('core').controller('Ctrl', function($scope, markerCache){
markerCache.cached = false; //should not be able to be set outside of markerCache service
});
Update: Here is a functional service for those interested
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
});
});