Horváth, Győző
senior lecturer
horvath.gyozo@inf.elte.hu
Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government
function ajax(opts) {
var method = opts.method || 'GET',
url = opts.url || '',
getdata = opts.getdata || '',
postdata = opts.postdata || '',
success = opts.success || function(){},
error = opts.error || function(){};
method = method.toUpperCase();
url = url+'?'+getdata;
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
if (method === 'POST') {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhr.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
success(xhr, xhr.responseText);
} else {
error(xhr);
}
}
};
xhr.send(method == 'POST' ? postdata : null);
return xhr;
}
$('#btnGet').on('click', function (e) {
ajax({
url: 'data1.json',
method: 'get',
success: function (data) {
data = JSON.parse(data);
console.log(data);
ajax({
url: 'data2.json',
method: 'get',
success: function (data) {
data = JSON.parse(data);
console.log(data);
}
});
}
});
});
$('#btnGet').on('click', function (e) {
$.ajax({
url: 'data1.json',
dataType: 'json',
type: 'get',
success: function (data) {
console.log(data);
var ref = data.ref;
$.ajax({
url: 'data2.json',
dataType: 'json',
type: 'get',
success: function (data) {
console.log(data);
}
});
}
});
});
Or: Pyramid of Doom
$('#btnGet').on('click', function (e) {
$.ajax({
success: function (data) {
$.ajax({
success: function (data) {
console.log(data);
}
});
}
});
});
$('#btnGet').on('click', onClick);
function onClick () {
$.getJSON('data1.json', processData1);
}
function processData1 (data) {
console.log(data);
var ref = data.ref;
$.getJSON('data2.json', processData2);
}
function processData2 (data) {
console.log(data);
}
$('#btnGet').on('click', onClick);
function onClick () {
$.getJSON('data1.json')
.then(processData1)
.then(processData2);
}
function processData1 (data) {
console.log(data);
var ref = data.ref;
return $.getJSON('data2.json');
}
function processData2 (data) {
console.log(data);
}
var promise = new Promise(
function (resolve, reject) { // (A)
...
if (...) {
resolve(value); // success
} else {
reject(reason); // failure
}
});
// Do not use this
promise.then(
function (value) { /* fulfillment */ },
function (reason) { /* rejection */ }
);
// Rather this one
promise.then(
function (value) { /* fulfillment */ }
);
promise.catch(
function (reason) { /* rejection */ }
);
// Definition
function httpGet(url) {
return new Promise(
function (resolve, reject) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.status === 200) {
// Success
resolve(this.response);
} else {
// Something went wrong (404 etc.)
reject(new Error(this.statusText));
}
}
request.onerror = function () {
reject(new Error(
'XMLHttpRequest Error: '+this.statusText));
};
request.open('GET', url);
request.send();
}
);
}
// Usage
httpGet('http://example.com/file.txt')
.then(
function (value) {
console.log('Contents: ' + value);
},
function (reason) {
console.error('Something went wrong', reason);
});
function ajax(opts) {
return new Promise(
function (resolve, reject) {
var method = opts.method || 'GET',
url = opts.url || '',
getdata = opts.getdata || '',
postdata = opts.postdata || '';
method = method.toUpperCase();
url = url+'?'+getdata;
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
if (method === 'POST') {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhr.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
resolve(xhr, xhr.responseText);
} else {
reject(xhr);
}
}
};
xhr.send(method == 'POST' ? postdata : null);
}
)
}
function getJSON (url) {
return new Promise(
function (resolve, reject) {
$.ajax({
url: url,
type: 'get',
dataType: 'json',
success: function (data) {
resolve(data);
},
error: function (xhr, status) {
reject(status);
}
})
}
);
}
function getJSON (url) {
return Promise.resolve($.getJSON(url));
}
$('#btnGet').on('click', onClick);
function onClick () {
getJSON('data1.json')
.then(processData1)
.then(processData2);
}
function processData1 (data) {
console.log(data);
var ref = data.ref;
return getJSON('data2.json');
}
function processData2 (data) {
console.log(data);
}
co
library$('#btnGet').on('click', onClick);
function onClick () {
co(function* () {
try {
var data1 = yield getJSON('data1.json')
var data2 = yield processData1(data1);
processData2(data2);
}
catch(msg) {
console.log(msg);
};
});
}
function processData1 (data) {
console.log(data);
var ref = data.ref;
return getJSON('data2.json');
}
function processData2 (data) {
console.log(data);
}
An object responsible for synchronization
Defaults to REST API
Overridable
create → POST /collection
read → GET /collection[/id]
update → PUT /collection/id
patch → PATCH /collection/id
delete → DELETE /collection/id
Need to set the url
field in the collection.
export default Backbone.Collection.extend({
url: '/api/codesnippets',
// ...
});
Sometimes need to set the id attribute in the model.
export default Backbone.Model.extend({
idAttribute: '_id',
// ...
});
Connect the code snippet client to the REST API!