I'm not quite making sense of where these Amadeus API Calls are coming from? - amadeus

I have a code here which is in .js, but some of these variable aren't making sense. For example, the line stating var departure_from = segment.departure.iataCode; there isn't anything in the file that the .js would understand departure_from or segment.departure.iataCode. I'm assuming that the Amadeus website interprets this, but I can't seem to find anything on the Amadeus website that would tell me which of these JS code snippets to use in which circumstances.
jQuery(function($){
$( "#departureDate" ).datepicker( { dateFormat: "yy-mm-dd" } );
$( "#returnDate" ).datepicker( { dateFormat: "yy-mm-dd" } );
$( "#flight_offers_request_form" ).submit( function( event ) {
event.preventDefault();
$( "#response" ).html( '' );
$.ajax({
url: ajax_object.ajax_url,
type: "post",
dataType: 'JSON',
data: $( this ).serialize(),
})
.done ( function( response ) {
var responseObj = $.parseJSON( response );
var content = '';
content += '<p>Flight offers from ' +
$( '#originLocationCode' ).val() +
' to ' + $( '#destinationLocationCode' ).val() +
' departing on ' + $( '#departureDate' ).val() +
( ( $( '#returnDate' ).val() !== '' ) ? ( ' and returning on ' + $( '#returnDate' ).val() ) : '') +
' for ' + $( '#adults' ).val() +
' adult' + ( $( '#adults' ).val() > 1 ? 's.' : '.' ) +
'</p>';
content += '<table>';
content += '<tr><th>ID</th><th>Departure Place</th><th>Departure Time</th><th>Arrival Place</th><th>Arrival Time</th><th>Flight No</th><th>Duration</th><th>Total Price</th></tr>';
$.each( responseObj.data, function( idx, data ) {
var id = data.id;
var currency = data.price.currency;
var total = data.price.total;
var segment_count = 0;
$.each( data.itineraries, function( idx, itinerary ) {
$.each( itinerary.segments, function( idx, segment ) {
var departure_from = segment.departure.iataCode;
var departure_time = segment.departure.at;
var arrival_at = segment.arrival.iataCode;
var arrival_time = segment.arrival.at;
var carrierCode = segment.carrierCode;
var number = segment.number;
var duration = segment.duration;
content += '<tr>';
content += '<td>' + ( ( segment_count === 0 ) ? id : '' ) + '</td><td>';
content += departure_from + '</td><td>' +
departure_time + '</td><td>' +
arrival_at + '</td><td>' +
arrival_time + '</td><td>' +
carrierCode + ' ' + number +
'</td><td>' +
duration + '</td>';
content += '<td>' + ( ( segment_count === 0 ) ? currency + ' ' + total : '' ) + '</td><td>';
content += '</tr>';
segment_count++;
})
})
})
content += '</table>';
$( '#response' ).html( content );
})
.fail(function( jqXHR, textStatus ) {
$( "#response" ).html( '<p>' + jqXHR.responseText + '</p>' );
})
})
});

Related

Vue js : window.addEventListener ne se trigger pas

My problem is that I am trying to get an access token from Spotify, but my window.addEventListener("message, function() {...}") is never triggered. Refer to the code below:
mounted(){
this.$nextTick(function() {
function login(callback) {
var CLIENT_ID = 'd3ebae5610894ca48c9f66794214252b';
var REDIRECT_URI = 'http://localhost:8080/spotify';
function getLoginURL(scopes) {
console.log('url')
return 'https://accounts.spotify.com/authorize?client_id=' + CLIENT_ID +
'&redirect_uri=' + encodeURIComponent(REDIRECT_URI) +
'&scope=' + encodeURIComponent(scopes.join(' ')) +
'&response_type=token';
}
var url = getLoginURL([
'user-read-email'
]);
var width = 450,
height = 730,
left = (screen.width / 2) - (width / 2),
top = (screen.height / 2) - (height / 2);
var w = window.open(url,
'Spotify',
'menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left
);
window.addEventListener("message", function(event) {
var hash = JSON.parse(event.data);
console.log(JSON.parse(event.data))
if (hash.type == 'access_token') {
callback(hash.access_token);
}
}, false);
}
var loginButton = document.getElementById('btn-login');
loginButton.addEventListener('click', function() {
console.log('click the button')
login(function(accessToken) {
console.log(accessToken)
});
});
})}

web3 - event.watch spits out events in random order

I am watching an event:
var events = EthProj.Message({}, { fromBlock: 0, toBlock: 'latest'});
events.watch((error, results) => {
Inside the event I do this tempString = ((messages.split(":")[1].split(",")[0] + " (From: " + messages.split(":")[2].split("}")[0]) + ")").replace(/"/g, ''); Which, long story short, makes a string from the event giving the event from the block (i.e. It gives the event made at block 173).
I then set a <h2> element's text from each event. When this happens it sets them in a seemingly random order. What can be going on, it sets them from block 0 to the latest block, so how can this happen.
Here is the full code: https://pastebin.com/wGt5kL1Y
var events = EthProj.Message({}, { fromBlock: 0, toBlock: 'latest'});
events.watch((error, results) => {
i++;
messages = "";
messages = JSON.stringify(results.args);
if(i === messageToGet) {
if(messages.split(":")[1].split(",")[0] != '""') {
console.log(messages.split(":")[1].split(",")[0] + " (From: " + messages.split(":")[2].split(",")[0].split("}")[0].split("}")[0] + ")");
tempString = ((messages.split(":")[1].split(",")[0] + " (From: " + messages.split(":")[2].split("}")[0]) + ")").replace(/"/g, '');
} else {
console.log("(No included text)" + "(From: " + messages.split(":")[2].split(",")[0].split("}")[0] + ")");
tempString = (("(no included text) " + " (From: " + messages.split(":")[2].split("}")[0]) + ")").replace(/"/g, '');
}
if((messages.split(":")[1].split(",")[0] === undefined) || (messages.split(":")[2].split(")")[0] === undefined)) {
return;
}
if(document.getElementById("Message" + placeToSet) != null) {
document.getElementById("Message" + placeToSet).remove();
if(document.getElementById("hr" + placeToSet) != null) {
document.getElementById("hr" + placeToSet).remove();
}
}
if(document.getElementById("Message" + placeToSet) === null) {
var newh2 = document.createElement('h2');
newh2.setAttribute("id", ("Message" + placeToSet));
var text = document.createTextNode(tempString);
newh2.appendChild(text);
document.body.appendChild(newh2);
var newHR = document.createElement('hr');
newHR.setAttribute("id", ("hr" + placeToSet));
}
}
});

Titanium Alloy - 'Global' Listener

I have multiple windows that 'require' livebar so that the entire bar persists over all windows. However, whenever the 'change' function is called, it works and logs, however my $.livebar_datalbl.text fails with the following error: "undefined is not an object (evaluating '$.livebar_datalbl.text = 'State: ' + e.description + ' (' + e.state + ')'')"
Am I structuring the code incorrectly or missing something?
index.js
(function constructor() {
audioPlayer = Ti.Media.createAudioPlayer({
url: 'https://allthingsaudio.wikispaces.com/file/view/Shuffle%20for%20K.M.mp3/139190697/Shuffle%20for%20K.M.mp3',
allowBackground: true
});
audioPlayer.addEventListener('progress', function(e) {
Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
});
audioPlayer.addEventListener('change', function(e) {
$.livebar_datalbl.text = 'State: ' + e.description + ' (' + e.state + ')';
Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
});
window = Alloy.createController('listen').getView();
window.open();
})();
livebar.xml
<Alloy>
<View class="livebar">
<View class="livebar_livelblcontainer">
<Label class="livebar_livelbl">LIVE</Label>
</View>
<Label class="livebar_datalbl" id="livebar_datalbl">HELLO WORLD</Label>
<ImageView id="livebar_playpausebtn" class="livebar_playpausebtn"/>
</View>
</Alloy>
livebar.js
$.livebar_playpausebtn.addEventListener('click', function(event) {
if (audioPlayer.playing || audioPlayer.paused) {
audioPlayer.stop();
if (Ti.Platform.name === 'android')
{
audioPlayer.release();
}
} else {
audioPlayer.start();
}
});
audioPlayer.addEventListener('progress', function(e) {
Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
});
audioPlayer.addEventListener('change', function(e) {
$.livebar_datalbl.text = 'State: ' + e.description + ' (' + e.state + ')';
Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
});
The audioPlayer.addEventListener event will only listen to events in the controller that you have created the audioPlayer in, in this case index.js. In your example the audioPlayer.addEventListener events in livebar.js have no effect as there is no audioPlayer to add event to.
If you would like to have the audioplayer in index.js and then have the livebar be updated and still keep the livebar in its own view+controller you will need to fire events across the controllers. To do this you can make use of Ti.App.fireEvent
You can read more here - Search for the "Application-Level Events" Section
http://docs.appcelerator.com/platform/latest/#!/guide/Event_Handling
You could do something like below.
Remember to be careful with App wide event listeners, you should always remove
those when you are done with them via the function below
Ti.App.removeEventListener("eventlistenername", eventfunctionname);
index.js
(function constructor() {
audioPlayer = Ti.Media.createAudioPlayer({
url: 'https://allthingsaudio.wikispaces.com/file/view/Shuffle%20for%20K.M.mp3/139190697/Shuffle%20for%20K.M.mp3',
allowBackground: true
});
audioPlayer.addEventListener('progress', function(e) {
Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
});
audioPlayer.addEventListener('change', function(e) {
// set livebareText
var livebareText = 'State: ' + e.description + ' (' + e.state + ')';
// fire app wide event
Ti.App.fireEvent("app:updateLivebar",livebareText);
Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
});
window = Alloy.createController('listen').getView();
window.open();
})();
livebar.js
$.livebar_playpausebtn.addEventListener('click', function(event) {
if (audioPlayer.playing || audioPlayer.paused) {
audioPlayer.stop();
if (Ti.Platform.name === 'android')
{
audioPlayer.release();
}
} else {
audioPlayer.start();
}
});
// Add App eventlistener to listen for updateSingleProgessBar
Ti.App.addEventListener("app:updateLivebar", updateLivebar);
function updateLivebar(livebarText){
$.livebar_datalbl.text = livebarText;
Ti.API.info('State: ' + e.description + ' (' + e.state + ')');
}

Vue js, cant pass data

I have simple deposit calc. Here is the code.
app.js:
el: '#calcbox',
data: {
newCalc: {
summ: '50000',
currency: 'USD',
duration: '',
percents: '',
},
calcResult: '',
},
computed: {
percents() {
var id = $("#deposit_id").val();
var url = "/get_percent/"+ id + '/' + this.newCalc.currency + '/' + this.newCalc.duration;
this.$http.get(url, function(response){
return this.newCalc.percents = response;
});
},
calcResult() {
var deposit_id = $("#deposit_id").val();
var url = "/api/calc/"+ deposit_id + '/' + this.newCalc.summ + '/' + this.newCalc.currency + '/' + this.newCalc.duration;
this.$http.get(url, function(response){
return this.calcResult = response;
});
}
},
frontend:
<span>#{{newCalc.percents}}%:</span>
<span>#{{calcResult}}:</span>
So the problem is that result isn't appears in frontend, console.log shows the correct result.
Percentage value is showing well.
calcResult() {
var deposit_id = $("#deposit_id").val();
var url = "/api/calc/"+ deposit_id + '/' + this.newCalc.summ + '/' + this.newCalc.currency + '/' + this.newCalc.duration;
this.$http.get(url, function(response){
return this.calcResult = response;
});
}
add .bind(this)
calcResult() {
var deposit_id = $("#deposit_id").val();
var url = "/api/calc/"+ deposit_id + '/' + this.newCalc.summ + '/' + this.newCalc.currency + '/' + this.newCalc.duration;
this.$http.get(url, function(response){
return this.calcResult = response;
}.bind(this));
}
Or use fat arrow notation since you're using ES6 syntax
calcResult() {
var deposit_id = $("#deposit_id").val();
var url = "/api/calc/"+ deposit_id + '/' + this.newCalc.summ + '/' + this.newCalc.currency + '/' + this.newCalc.duration;
this.$http.get(url, response => {
return this.calcResult = response;
});
}
the this in the .then callback is not the same context as the one outside or within your vue method

Render HTML with images using PhantomJS

I am trying to create a PDF from HTML text using PhantomJS (version 1.9.7). I've written a very simple script (made more complicated by error callbacks etc.)
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
});
}
system.stdout.write(msgStack.join('\n'));
phantom.exit(1);
};
var page = require('webpage').create();
page.viewportSize = { width: 800, height: 600 };
page.paperSize = { format: 'A4', orientation: 'portrait', margin: '1cm' };
page.onResourceRequested = function(requestData, networkRequest) {
console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};
page.onResourceReceived = function(response) {
console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
});
}
console.error(msgStack.join('\n'));
};
page.content = "<html><body><b>test</b><img src=\"http://www.google.co.uk/images/srpr/logo11w.png\" alt=\"\" border=\"0\" /></body></html>";
page.render('tmp.pdf');
setTimeout(function() {
phantom.exit();
}, 5000);
I set up the page, assign the simple HTML string to the content property and render it to a PDF.
This script doesn't produce any output.
I've narrowed the problem down to the <img> element, when that is removed a PDF is generated as expected. I can see from the callback functions that the image is requested, a response is received, and there are no errors reported. I've tried rendering to a PNG which also yields no output.
I've explored the possibility of this being a proxy issue, however the raserize.js example works without any problems.
You have to call render when the page is fully loaded. Remember that loading a page via page.open or page.content is always async.
Change your code to this
page.content = "<html><body><b>test</b><img src=\"http://www.google.co.uk/images/srpr/logo11w.png\" alt=\"\" border=\"0\" /></body></html>";
setTimeout(function() {
page.render('tmp.pdf');
phantom.exit();
}, 5000);