0

I have this object:

var crudConfig = function($wizard, $formModal, $deleteModal) {

'use strict';

return {


    handleOnShowFormModal : function() {

        $formModal.on('show.bs.modal', function(event) {
               ...................
                    this.fillForms(data);
               ....................
        });

        return this;
    },
    fillForms : function(data) {
        //do stuff
        return this;
    }
 }
}

The problem appears when I call the fillForms with the param.

Uncaught TypeError: this.fillForms is not a function

As the fillForms key is an anonymous function how can I call it from inside the object? On other relative questions I only found how to refer itself if the key has a string value and the I call like this: this.fillForms .

  • You should use a constructor instead of factory function. – Redu Jun 18 '16 at 21:02
  • 2
    Possible duplicate of [How to access the correct \`this\` / context inside a callback?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) – melpomene Jun 18 '16 at 21:03

1 Answers1

2

this within the callback references the $formModal element. What you need to do is store this that refer to the object in a variable before the event listener is called and use the variable within the callback to access the object.

Just like this:

handleOnShowFormModal : function() {
  var _this = this
  $formModal.on('show.bs.modal', function(event) {
    _this.fillForms(data); 
  });

  return this;
},
El'Magnifico
  • 798
  • 6
  • 15