Backbone:
var backboneSync = Backbone.sync;
Backbone.sync = function (method, model, options) {
/*
* The jQuery `ajax` method includes a 'headers' option
* which lets you set any headers you like
*/
options.headers = {
/*
* Set the 'Authorization' header and get the access
* token from the `auth` module
*/
//'Authorization': 'Bearer ' + auth.getAccessToken()
};
options.beforeSend = function(xhr, settings) {
$('#loader').show();
settings.xhr = function() {
var xhr = $.ajaxSettings.xhr();
//xhr.setRequestHeader('X-Auth-Token_or_other_header' , your_hash_key)
xhr.addEventListener("progress", function (event) {
var val = Math.ceil(event.loaded/event.total*100);
helper.loader(val);
console.log(val);
}, false);
return xhr;
}
}
/*
* Call the stored original Backbone.sync method with
* extra headers argument added
*/
return backboneSync(method, model, options);
};
Javascript:var tmdb_id = $('#movie_selected').val();
var movie = new model.Movie({ });
movie.save({
tmdb_id: tmdb_id
},
{
beforeSend: function (xhr, options) {
// this method doesn't work
console.log('before send'); // Will be "Hello" when collection fetched
},
success: function(model, response, xhr) {
$('button#preview-movie').prop('disabled', false);
$('button#delete-movie').prop('disabled', false);
},
error: function(e, response, xhr) {
console.log('error = ')
console.log(response)
}});
Laravel:public function store(Request $request) {
ini_set('max_execution_time', 480);
mass execution of saving data and images lasts about 3 minutes
}
Laravel middleware:use Closure;
class AddContentLength {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
// to be sure nothing was already output (by an echo statement or something)
if (headers_sent() || ob_get_contents() != '') return $response;
if (str_contains($response->headers->get('content-type'), 'application/json')) {
$content = $response->content();
$contentLength = strlen($content);
$response->header('Content-Length', $contentLength);
$response->header('Accept-Ranges', 'bytes');
$response->header('X-Content-Length', $contentLength);
$response->header('Content-Type', 'application/json');
}
// $useCompressedOutput = ($contentLength && isset($_SERVER['HTTP_ACCEPT_ENCODING']) &&
// strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false);
// if ($useCompressedOutput) {
// // In order to accurately set Content-Length, we have to compress the data ourselves
// // rather than letting PHP do it automatically.
// $compressedContent = gzencode($content, 9, FORCE_GZIP);
// $compressedContentLength = strlen($compressedContent);
// if ($compressedContentLength/$contentLength < 0.9) {
// if (ini_get('zlib.output_compression')) ini_set('zlib.output_compression', false);
// $response->header('Content-Encoding', 'gzip');
// $response->setContent($compressedContent);
// $contentLength = $compressedContentLength;
// }
// }
// compressed or not, sets the Content-Length
return $response;
}
}via ONYX