Skip to content

Instantly share code, notes, and snippets.

@IcodeNet
Created January 21, 2014 08:12
Show Gist options
  • Save IcodeNet/8536139 to your computer and use it in GitHub Desktop.
Save IcodeNet/8536139 to your computer and use it in GitHub Desktop.
How do I encapsulate in Knockout all the POST, GET, DELETE operations? ViewModelHelper
window.myApp = window.myApp || {};
(function (ns) {
var rootPath;
ns.rootPath = rootPath;
}(window.myApp));
(function (ns) {
var viewModelHelper = function () {
var self = this;
self.modelIsValid = ko.observable(true);
self.modelErrors = ko.observableArray();
self.isLoading = ko.observable(false);
self.apiGet = function (uri, data, sucessCallback, failureCallback, alwaysCallback) {
self.isLoading(true);
self.modelIsValid(true);
$.get(myApp.rootPath + uri, data)
.done(sucessCallback)
.fail(function (result) {
if (failureCallback == null) {
var msg = result;
if (result.status != 400) {
self.modelErrors([result.status + ':' + result.statusText + ':' + result.responseText]);
msg = result.responseText;
} else {
self.modelErrors(JSON.parse(result.responseText));
msg = JSON.parse(result.responseText);
}
toastr.options = {
"closeButton": true,
"timeOut": "0",
"extendedTimeOut": "0"
};
toastr.error(msg, "Result");
self.modelIsValid(false);
} else {
failureCallback(result);
}
})
.always(function () {
if (alwaysCallback == null) {
self.isLoading(false);
} else {
alwaysCallback();
}
});
};
self.apiPost = function (uri, data, sucessCallback, failureCallback, alwaysCallback) {
self.isLoading(true);
self.modelIsValid(true);
$.post(myApp.rootPath + uri, data)
.done(sucessCallback)
.fail(function (result) {
if (failureCallback == null) {
if (result.status != 400)
self.modelErrors([result.status + ':' + result.statusText + ':' + result.responseText]);
else {
self.modelErrors(JSON.parse(result.responseText));
}
self.modelIsValid(false);
} else {
failureCallback(result);
}
})
.always(function () {
if (alwaysCallback == null) {
self.isLoading(false);
} else {
alwaysCallback();
}
});
};
self.apiDelete = function (uri, data, sucessCallback, failureCallback, alwaysCallback) {
self.isLoading(true);
self.modelIsValid(true);
$.ajax({ type: 'DELETE', url: myApp.rootPath + uri + "?id=" + data.id, data: data })
.done(sucessCallback)
.fail(function (result) {
if (failureCallback == null) {
if (result.status != 400)
self.modelErrors([result.status + ':' + result.statusText + ':' + result.responseText]);
else {
self.modelErrors(JSON.parse(result.responseText));
}
self.modelIsValid(false);
} else {
failureCallback(result);
}
})
.always(function () {
if (alwaysCallback == null) {
self.isLoading(false);
} else {
alwaysCallback();
}
});
};
};
ns.ViewModelHelper = viewModelHelper;
}(window.myApp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment