James123
James123

Reputation: 11652

Object doesn't support property or method 'save'

I am getting error while calling reportManager.save("/EditInitiatives.svc/SaveGridData"); method

<script type='text/javascript'>
$(function () {
    $('#saveReport, #save-report-bottom').click(function () {
        $.blockUI({ message: '<h4><img src="/Images/busy.gif" /> Saving your changes.<br/>This operation might impact several reports at once.<br/>Please be patient.</h4>' });
        uiController.disableSaveButton();
        reportManager.save("/EditInitiatives.svc/SaveGridData");
    });

    var reportManager = function () {
        var tableData = JSON.stringify(handsontable.getData());
        var input = JSON.stringify({ "input": tableData });
        alert("Data" +  input); 

        save = function(saveUrl) { 
            alert("save" + saveUrl);

            $.ajax({
                url: saveUrl,
                type: 'POST',
                dataType: 'json',
                data: input, //returns all cells' data
                contentType: 'application/json; charset=utf-8',
                success: function(res) {
                    if (res.result === 'ok') {
                        console.text('Data saved');
                    }                        
                    $.unblockUI();                            
                }, 
                error: function (xhr) {
                    alert(xhr.responseText);
                }                       
            });
        }
    };
}
</script>

Upvotes: 3

Views: 1032

Answers (2)

Arash Milani
Arash Milani

Reputation: 6308

You have declared the save function without var keyword; so it will be declared in global scope not as function of reportManager.

Even if you put the var keyword before save function then it's not accessible from outside of reportManager function scope. to expose it to public you need kinda export it. here is a simple pattern to do it:

 var reportManager = (function (self) {

      function save(saveUrl) { ... } //this function is not public yet

      self.save = save; //we are exporting the save function here
      return self; //now we are returning an object with save function

 })(reportManager || {});


 reportManager.save("your-param-here"); //Now use your function the way you want

Upvotes: 0

epascarello
epascarello

Reputation: 207527

You can not access it since save is a Global variable and not part of reportManager.

The reason why it is global is because you are missing the var in front of the variable. That puts it into the Global Namespace. It will not magically be hooked up to the block scope of the function. You would need to use an OO approach to get that to work. Some basic ideas are

function Report() {
     var x = 1;
    this.save = function () {
        alert("First: " + x);   
    }
}

var report = new Report();
report.save();


function report_2() {
     var x = 1;
    return {
        save : function () {
            alert("Second: " + x);   
        }
    }
}

var report2 = report_2();
report2.save();


var report_3 = (function () {
     var x = 1;

    var returnObj = {};
    returnObj.save = function () {
            alert("Third: " + x);   
    }

    return returnObj;


    //return {
    //    save  :  function() {
    //        alert("Third: " + x);   
    //    }
    //}
})();

report_3.save();

Fiddle of Examples: http://jsfiddle.net/5Zhsq/

Upvotes: 3

Related Questions