1

I have been looking at this document:

understanding-service-types

Because I am new to AngularJS I am having some problems understanding everything in there. I still don't understand the difference between a factory and a service, but I will leave that for another day.

The problem I have now, is that I created a model as a factory and now I think I may have done it wrong.

Here is my model:

commonModule.factory('optionsModel', function () {
    var _options = angular.fromJson(sessionStorage.siteOptions);
    var _defaults = {
        rotateBackground: false,
        enableMetro: true
    };

    if (_options) {
        _defaults.rotateBackground = _options.rotateBackground;
        _defaults.enableMetro = _options.enableMetro;
    }

    var _save = function (options) {
        console.log(options);

        sessionStorage.siteOptions = angular.toJson(options);
    }

    return {
        options: _defaults,
        save: _save
    };
});

As you can see here, what I am doing is setting the defaults and then I check to see if we have anything in our session, if we do I then overwrite our options with the new settings. I also have a save function which is used to save the options to the session.

Is this the best way to make this model or should I be doing it another way?

3
  • Does it work? Are any errors thrown? What makes you think you may have done it wrong? Surely there are other ways to do what you are doing, but only you can decide which one is "Best" for your use case. Commented Mar 11, 2015 at 16:46
  • well, yeah it works. But I read that a service instantiates itself. If that's the case, then I could just put that code into the constructor. Also, I think they may be an angular version of jquery extend which I could use for the options. I am going to have another look and see if I can answer this myself. Commented Mar 11, 2015 at 16:53
  • You are using a factory, not a service. You could change it to a service, but one isn't necessarily better than the other. Just different. Commented Mar 11, 2015 at 16:53

1 Answer 1

1

I don't think you should think about a model in the way you're doing it.

For your purpose, you can do it in a more "angular" way :

commonModule.factory('optionsModel', function () {
   var factory = {
       getOptions: getOptions,
       saveOptions: saveOptions
   }

   // If you need default values, you can assign those here, 
   // but you can also think about adding a dependency into your factory, 
   // that would be bound to your default settings.

   return factory;

   function getOptions(){
       return angular.fromJson(sessionStorage.siteOptions);
   }

   function saveOptions(options){
        sessionStorage.siteOptions = angular.toJson(options)
   }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.