0

I'm trying to call another function within my factory but i get this error. i did what others said in this link. i could not find the problem. where did i go wrong? Thanks!

enter image description here

.factory('UserPreferences',function(Http,ngDialog,Notification){
        var settings = {}

  return {
    authenticated: false,
    settings:{},        
    saveSettings: function(data){
        var data = {
            url: '/pointofsale/userpreferences/save',
            data: data
        }

        Http.Post(data)

            .success(function(data){
                statusMessage = data
                ngDialog.closeAll(0)
            })
    },
    authenticate_change_settings: function(user,settings){
        this.settings = settings

        var data = {
            url: '/pointofsale/authenticate_change_settings',
            data: user
        }

        Http.Post(data)
            .success(function(data){

                if(data){
                    this.authenticated = true;
                    this.saveSettings(this.settings);

                }
            }).error(function(data){    
                Notification.error(data)
            })

    }       
}

Update: so i found the problem, i can't call the function within the $http post call success function. why is this happening?

So, i tried doing this. and it works, it called the function. not sure if this is proper.

authenticate_change_settings: function(user){
    var $this = this;

    var data = {
        url: '/pointofsale/authenticate_change_settings',
        data: user
    }

    Http.Post(data)
        .success(function(data){

            if(data){
                $this.authenticated = true;
                $this.saveSettings(this.settings);

            }
        }).error(function(data){    
            Notification.error(data)
        })

}
Community
  • 1
  • 1
Binsoi
  • 263
  • 4
  • 13
  • what method are you returning from factory ? and why does it have different name with underscore ? – ProllyGeek Aug 02 '16 at 02:34
  • oops, sorry i edited it. i type the wrong function name when transferring the code snippet – Binsoi Aug 02 '16 at 02:37
  • Seems to be a 'this' issue http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – MMhunter Aug 02 '16 at 03:20
  • @Binsoi be cause you cant call anything after you return an object, anyway http is async call it before you return your factory object. – ProllyGeek Aug 02 '16 at 03:26
  • so i tried putting var $this = this inside the authenticate_change_settings() then i replaced this.saveSettings() with $this.saveSettings() and it works. i'm not sure if this is correct or proper – Binsoi Aug 02 '16 at 03:31

0 Answers0