I love that CODE.org has a handful of built-in (faux?) data services hiding behind the DATA tab!
I am noticing 2 things:
readRecords requires an asynchronous callback, but getColumn does not. How does that work, and if getting columns is not a separate request, is there a way to get all the columns for a table?
readRecords works as I would expect, with a callback on connection, but doesn’t seem to include any information about which request has answered the callback. If that is the case, is there a way to fire multiple requests to different databases and identify which payload is answering which request?
As an example, if I want to mix data from the “Dogs” table and the “Countries and Provence” table I would like to know that both requests are complete before beginning my data manipulation.
The difference is as follows, readRecords tells you when the data is available through a callback. If you want to know when two pieces of data are available just wait till the last of the two finishes to start to process the data.
getColumn uses a built in callback function written in the native Javascript. That means it runs about 200 times faster than any callback function you can write. That reduces the risk of trying to use data that isn’t there yet but doesn’t eliminate it.
Do you want fast or reliable?
I would just do something like this:
var HistoricalNonViolentResistances;
var Dogs;
var historicalNonViolentDone = false;
var dogsDone = false;
readRecords("Historical Non-violent Resistances",{}, function (data) {
historicalNonViolentDone = true;
HistoricalNonViolentResistances = data;
if (dogsDone) {
doIt();
}
});
readRecords("Dogs",{}, function (data) {
dogsDone = true;
Dogs = data;
if (historicalNonViolentDone) {
doIt();
}
});
function doIt(){
console.log(HistoricalNonViolentResistances[HistoricalNonViolentResistances.length-1]);
console.log(Dogs[Dogs.length-1]);
}
@jdonwells , thx for the quick reply
So if “getColumn” event sometimes works, that must mean that every call execution must be delayed?
For context, I am kick-the-tiers extra hard between semesters. I really like the ramp-up path AppLab offers. But there seem to be a couple of places where it creates a false illusion of how client programming works (like this getColumn that seems to work in-line, when it really doesn’t).
…thus, I appreciate your getting back to me and helping me talk through the trade-offs of the AppLab vs going with bare-bones JS.
@jdonwells , it sounds like you would not recommend using the JS Object.bind() as a means of adding a bit more robustness to CallBacks?
Is GameLab better in this respect?
Can I use the Data from AppLabs in GameLab?
One thing to keep in mind is that the Javascript interpreter used by App Lab was written by some guy years ago. To me, it looks like that project is largely abandoned. So anything not in the App Lab documentation is not used often. Anything that is not used often is not tested fully. Anything that is not tested fully may have bugs.
AP CSP which uses App Lab never really talks about callbacks explicitly that I have seen. We drag onEvent() over and don’t even notice it comes with a function definition. getColumn() tries to eliminate the need to discuss callbacks by giving us an operation that is generally fast enough on small datasets that we can ignore the potential race condition (if we follow some simple rules.)
Largely I would say it works out fine to ignore event driven programming as a given. Consider for example Scratch and Snap! which take multitasking as a given. Try explaining concurrency issues to a student. I would rather explain events.
Most of what you would want with bind() can be done simply without it. Worst case you can use a closure. That seems to come more naturally and the students seem to understand the idea without talking about it in detail beyond the class scope.
Game Lab uses the same interpreter as App Lab, but with different built in functions. It is designed for games as you may have guessed. It doesn’t have the datasets App Lab has because they have very different purposes and curriculums as targets.
What a bummer that they aren’t investing anything in the coding platform anymore. It is easy enough to get the JS engine by exporting a project. Do you happen to know if it has already been open-sourced and if so, if anyone might be around to take pull requests?
In answer to my own question, AppLab does have event binding, but it is hidden.
OnEvent has a secret 4th parameter, AKA any information you would like to bind to the callback. You can use that property to help identify which requests have completed their asynchronous action.
It’ll post an example later, and how this relates to APCS-Princ questions about user defined procedures and parameter passing.