调用reportManager.save("/EditInitiatives.svc/SaveGridData");
方法
<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>
答案 0 :(得分:3)
您无法访问它,因为save
是全局变量而不是reportManager
的一部分。
它是全局的原因是因为你缺少变量前面的var。这将它放入全局命名空间。它不会神奇地连接到函数的块范围。您需要使用OO方法才能使其工作。一些基本想法是
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();
答案 1 :(得分:0)
您已声明save
函数没有var
关键字;因此它将在全局范围内声明,而不是reportManager
的函数。
即使您在var
函数之前放置了save
关键字,也无法从reportManager
函数scope
之外访问它。要公开它,你需要有点export
它。这是一个简单的模式:
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