1

I'm creating an angular factory that encapsulates and injectable type that can be new-ed up as such:

(function() {
angular
  .module('app')
  .factory('PaymentStream', [

function() {

    function PaymentStream(){

        this._endingBalance = null;
    }

    PaymentStream.prototype.serialize = function() {

        // method body here
    };

    return PaymentStream;

   }]);
})();

When I create a PaymentStream via new PaymentStream() only the constructor seems to have been called. If I don't use prototype and just define the methods inside the constructor it works but then I'm redefining the functions with each instance.

Any idea why those prototypes are not being set?

Edit

Here is how it is being used in the outside.

    function addStreamFunctions(stream) {

        angular.merge(stream, new PaymentStream());
    }
1
  • I don't see anything wrong with your code, maybe the way you are using the factory outside is the problem? can you show how you use it from a controller or something? Commented Feb 5, 2016 at 4:41

2 Answers 2

1

So basing on your edit, The factory was indeed working correctly. The problem is the way you used it, you angular.merged the newed object with another object, so its type is not a PaymentStream anymore, the resulting object became a 'generic' object

If the stream is a plain object, what you can do is pass the stream variable in the constructor of PaymentStream and do the merging manually inside your constructor function. Something like

function PaymentStream(stream){

    this._endingBalance = null;

    var keys = Object.keys(stream); //get all properties from the stream

    keys.forEach(function(key){
       this[key] = stream[key];   //put each one in the instance of your object;
    });

}

then use it like

function addStreamFunctions(stream) {

    var stream = new PaymentStream(stream);

}

this code is untested, please tell me if it worked or not :)

Sign up to request clarification or add additional context in comments.

Comments

0

Since you are working with factory you need to return an instance of the object, in this case a new instance of PaymentStream

(function() {
  angular.module('my-app').factory('PaymentStream', [function() {
    function PaymentStream() {
      this._endingBalance = null;
    }

    PaymentStream.prototype.serialize = function() {
      // method body here
    };

    return new PaymentStream();
  }]);
})();

4 Comments

I think he wants the class to be returned by the factory, not an instance
@LouieAlmeda jsfiddle.net/arunpjohny/pad6zsvp/3 - In that case OP's code should just work
Yes it does, I don't see anything wrong about the code either, maybe we're looking at the wrong portion of the code
@LouieAlmeda - that's exactly right, I'm looking to take the factory and use it o new up classes

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.