2011-11-25 01:04:42 +00:00
|
|
|
(function() {
|
|
|
|
var methodMap = {
|
|
|
|
'create': 'POST',
|
|
|
|
'update': 'PUT',
|
|
|
|
'delete': 'DELETE',
|
|
|
|
'read' : 'GET'
|
|
|
|
};
|
|
|
|
|
|
|
|
var getUrl = function(object) {
|
|
|
|
if (!(object && object.url)) return null;
|
|
|
|
return _.isFunction(object.url) ? object.url() : object.url;
|
|
|
|
};
|
|
|
|
|
|
|
|
var urlError = function() {
|
|
|
|
throw new Error("A 'url' property or function must be specified");
|
|
|
|
};
|
|
|
|
|
|
|
|
Backbone.sync = function(method, model, options) {
|
|
|
|
var type = methodMap[method];
|
|
|
|
|
|
|
|
// Default JSON-request options.
|
|
|
|
var params = _.extend({
|
|
|
|
type: type,
|
|
|
|
dataType: 'json',
|
|
|
|
beforeSend: function( xhr ) {
|
|
|
|
var token = $('meta[name="csrf-token"]').attr('content');
|
|
|
|
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
|
|
|
|
}
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
if (!params.url) {
|
|
|
|
params.url = getUrl(model) || urlError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we have the appropriate request data.
|
|
|
|
if (!params.data && model && (method == 'create' || method == 'update')) {
|
|
|
|
params.contentType = 'application/json';
|
|
|
|
|
|
|
|
var data = {}
|
|
|
|
|
|
|
|
if(model.paramRoot) {
|
|
|
|
data[model.paramRoot] = model.toJSON();
|
|
|
|
} else {
|
|
|
|
data = model.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(FormData) != 'undefined') { // XHR2
|
|
|
|
var formData = new FormData();
|
|
|
|
|
2011-12-02 17:20:59 +00:00
|
|
|
formData.append('xhr', true); // fix a bug with POW which happens to not like empty form
|
2012-02-10 00:46:35 +00:00
|
|
|
if (typeof(window.content_locale) != 'undefined' && window.content_locale != '')
|
2012-02-10 00:42:06 +00:00
|
|
|
formData.append('content_locale', window.content_locale);
|
2011-12-02 17:20:59 +00:00
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
var _buildParams = function(prefix, obj, fn) { // code grabbed from jquery
|
|
|
|
if (jQuery.isArray(obj)) {
|
2012-01-10 01:24:34 +00:00
|
|
|
if (obj.length == 0) { // empty arrays
|
|
|
|
fn(prefix, obj);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
jQuery.each(obj, function(i, v) {
|
|
|
|
if (/\[\]$/.test(prefix)) { // rbracket
|
|
|
|
fn(prefix, v);
|
|
|
|
} else {
|
|
|
|
_buildParams(prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v, fn);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (obj != null && typeof obj === "object" && !(obj instanceof File)) {
|
|
|
|
for (var name in obj) {
|
|
|
|
_buildParams(prefix + "[" + name + "]", obj[name], fn);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fn(prefix, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var prefix in data) {
|
|
|
|
_buildParams(prefix, data[prefix], function(key, value) {
|
2012-02-03 00:50:45 +00:00
|
|
|
// console.log('append ' + key + ', ' + value + ', is Array = ' + jQuery.isArray(value));
|
|
|
|
if (jQuery.isArray(value) && value.length == 0)
|
|
|
|
formData.append(key + '[]', '');
|
|
|
|
else
|
|
|
|
if (value != null)
|
|
|
|
formData.append(key, value);
|
2011-11-25 01:04:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
params.data = formData;
|
|
|
|
params.processData = false;
|
|
|
|
params.contentType = false;
|
|
|
|
} else {
|
|
|
|
params.data = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't process data on a non-GET request.
|
|
|
|
if (params.type !== 'GET') {
|
|
|
|
params.processData = false;
|
|
|
|
}
|
|
|
|
|
2011-12-06 11:39:32 +00:00
|
|
|
params.complete = function(xhr, textStatus) {
|
|
|
|
var noticeType = xhr.getResponseHeader('X-Message-Type');
|
|
|
|
if (noticeType != null) {
|
|
|
|
var growlType = noticeType == 'notice' ? 'notice' : 'alert'; // for now, only 2 kind of growl messages
|
|
|
|
$.growl(noticeType, xhr.getResponseHeader('X-Message'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-25 01:04:42 +00:00
|
|
|
// Make the request.
|
|
|
|
return $.ajax(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
}).call(this);
|