views:

112

answers:

1

I have a singleton object that use another object (not singleton), to require some info to server:

var singleton = (function(){

  /*_private properties*/
  var myRequestManager = new RequestManager(params,
    //callbacks
    function(){
        previewRender(response);
    },
    function(){
        previewError();
    }
  );

  /*_public methods*/
  return{

    /*make a request*/
    previewRequest: function(request){
       myRequestManager.require(request);  //err:myRequestManager.require is not a func
    },

    previewRender: function(response){
      //do something
    },

    previewError: function(){
      //manage error
    }
  };
}());

This is the 'class' that make the request to the server

function RequestManager(params, success, error){
  //create an ajax manager
  this.param = params;
  this._success = success;  //callbacks
  this._error = error;
}

RequestManager.prototype = {

  require: function(text){
    //make an ajax request
  },
  otherFunc: function(){
     //do other things
  }

}

The problem is that i can't call myRequestManager.require from inside singleton object. Firebug consolle says: "myRequestManager.require is not a function", but i don't understand where the problem is. Is there a better solution for implement this situation?

+5  A: 
T.J. Crowder
+1 JavaScript hoisting trap strikes again!
bobince
Thanks for explanation and examples, I didn't get the difference between prototype augmented and prototype replaced.Now it works :-)
Manuel Bitto
@Kucebe: Great! Yeah, it's a subtle one. :-) Not just prototypes, it's the order in which things happen that'll bite you sometimes (it certainly has *me*).
T.J. Crowder