Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS "Constant" Factory

I'm working on setting up configuration files in AngularJS. Is it possible to create a factory in AngularJS whose key cannot be overwritten? For example, I can set up a constant like this, which cannot be overwritten

module.constant('animals', {"cat": "meow", "dog": "woof"});

But I'd like to do something like this that allows for overwritten values but not the factory itself

module.value('catSound','meow')
      .value('dogSound','woof')
      .factory('animals', ['catSound','dogSound', function(catSound, dogSound) {
            return {
                 "cat": catSound,
                 "dog": dogSound
            }
      });

The above factory can be overwritten allowing for another piece of code to have module.factory('animals',function(){ return 7 }) and break everything. However, as a factory, the individual values can (and should) be overwritable, so I should be able to assign module.value('catSound','hiss') and have things still work as expected.

I've tried injecting into constants, but as far as I've been able to understand that isn't possible. How can I prevent my factory declaration from being overwritten? I realize that constant probably isn't the correct term when describing what I want, but I do want the factory's function definition to be constant.

like image 251
Ian Hunter Avatar asked Mar 01 '13 16:03

Ian Hunter


People also ask

What is constant in AngularJS?

Constant are like services in AngularJS in which we can define our globally data. It is declare using "constant" keyword. As we define our app-keys in Web.

What is the factory function in AngularJS?

Factory is an angular function which is used to return the values. A value on demand is created by the factory, whenever a service or controller needs it. Once the value is created, it is reused for all services and controllers. We can use the factory to create a service.

What is difference between factory service and provider in AngularJS?

The major difference between an AngularJS service and an AngularJS factory is that a service is a constructor function and a factory is not. That is why, in the case of a factory, we return an object literal instead of using this.

What is $resource in AngularJS?

Overview. A factory which creates a resource object that lets you interact with RESTful server-side data sources. The returned resource object has action methods which provide high-level behaviors without the need to interact with the low level $http service. Requires the ngResource module to be installed.


1 Answers

Everything is mutable in javascript, so setting up something like this is tricky and never completely fail-safe. I've looked around in the angular code and found no evidence of any attempt at a protection mechanism like you seem to be asking for.

My advise would be to just live with the risk. It's not worth trying to protect yourself other than with tests and nice long names. I assume you've been bitten once, but I think the chances are quite low, unless you define factories with very short names.

like image 120
iwein Avatar answered Sep 29 '22 20:09

iwein