Skip to content

Instantly share code, notes, and snippets.

@wesen
Created August 22, 2011 23:45
Show Gist options
  • Save wesen/1163937 to your computer and use it in GitHub Desktop.
Save wesen/1163937 to your computer and use it in GitHub Desktop.
Deferred JSON with jquery / backbone.js
var App = {
Views: {
Member: {},
Admin: {},
Misc: {},
Cart: {},
Checkout: {}
},
Collections: {},
Controllers: {},
Data: {},
notifyUser: function (type, data) {
var templateName = "notification/" + type;
loadTemplates([templateName])
.then(function () {
alert("notification for " + type + ": " + ich["notification_" + type](_.defaults(data, tplBaseData), true));
});
},
notifyErrors: function (errors) {
App.notifyUser("error", { "error": errors.join("\n") });
},
redirect: function (url, params) {
redirectTo(getSiteUrl(url), params);
},
/**
* Do a GET request to the app RestServer.
**/
GET: function (url, params) {
var dfd = $.Deferred();
$.getJSON(getSiteUrl(url, params))
.then(function (res) { parseJSONResults(res, dfd); })
.fail(function () { dfd.reject(["Network error"]); });
return $.when(dfd.promise());
},
/**
* Do a POST request to the app RestServer.
**/
POST: function (url, data) {
var dfd = $.Deferred();
$.postJSON(getSiteUrl(url), data)
.then(function (res) { parseJSONResults(res, dfd); })
.fail(function () { dfd.reject(["Network error"]); });
return $.when(dfd.promise());
},
/**
* Do a PUT request to the app RestServer.
**/
PUT: function (url, data) {
var dfd = $.Deferred();
$.putJSON(getSiteUrl(url), data)
.then(function (res) { parseJSONResults(res, dfd); })
.fail(function () { dfd.reject(["Network error"]); });
return $.when(dfd.promise());
},
/**
* Do a DELETE request to the app RestServer.
**/
DELETE: function (url) {
var dfd = $.Deferred();
$.deleteJSON(getSiteUrl(url))
.then(function (res) { parseJSONResults(res, dfd); })
.fail(function () { dfd.reject(["Network error"]); });
return $.when(dfd.promise());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment