Javascript 'class' and singleton problems

Posted by Kucebe on Stack Overflow See other posts from Stack Overflow or by Kucebe
Published on 2010-04-19T12:25:57Z Indexed on 2010/04/19 12:53 UTC
Read the original article Hit count: 218

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about closure