Category Archives: Monday Tips
Hi,
maybe this could sound obvious but is something that i didn’t know till a few days ago when i was making some changes on a js code i made. like the title says, the each() function used in mootools to apply a code to each element in the array, do not work with associative arrays.
so basically if you have this code:
var myArray=new Array();
myArray["key1"]="value1";
myArray["key2"]="value2";
myArray.each(function (item, index){
//do stuff
});
will not work, simply will not execute the loop on each array element. to fix this, you need to use the for loop like this:
var myArray=new Array();
myArray["key1"]="value1";
myArray["key2"]="value2";
for(key in myArray){
//do stuff
}
dunno if this is a mootools bug or was designed to work like this, but i hope this help you and save you time in debugging when you are using associative arrays.
update: from the comments below, let’s clarify some things:
*there is no associative arrays in javascript, they are objects, and i was wrong, indeed you can process it using the solution that Dimitar Christoff posted
var myArray= {};
myArray["key1"]="value1";
myArray["key2"]="value2";
Object.each(myArray, function (item, key){
console.log(item, key);
});
*when you process the object like this, you need to check that you are not processing the js methods that mootools define, personally i stop processing the object after i found the “$family” key, in the comments recommend to use the hasOwnProperty() function but i don’t see why is better or different than mine, if you know it, post it
thanks for the comments, i recognize when i’m wrong, for your collaboration, i’m a bit a better dev
Regards,
Shadow.
Related Post
Hi,
for the past days i have been playing with selenium, coding test’s for the application we are developing. in my journey with this great tool i have found some interesting tips that could be handy to you in my own situation.
Handle ajax calls
there are 2 ways to handle ajax calls, one is making the actions that will trigger the ajax call (like a click or something) and set a pause for XXXX quantity of miliseconds and then check that the actions has been made. the other is the waitForCondition command, that will execute the script till the condition is true. the second is the best,you can check a condition by setting up a variable or checking for some stat on the page after the ajax call is made. be sure to measure how much time the ajax call take action on a normal environment so you can set up a correct timeout.
Access to content in the page for a conditional or a script
there are commands that require you write some conditional sentence or minimal script to verify an action. in some of this cases is probably you need to get some value for a element in the page. in this case you can use this function:
this.page().findElement('locator');
this will return you the object like a getElementById will, and support the use of locators.
Store the result of a script
maybe you need to save the result of a script for later, so, you can use storeEval for that purpose. will store the result of the last line of the script on a variable you define.
later you can access to it like this:
storedVars['foo'];
i hope it help you in some way
Regards,
Shadow.
Related Post
Hi,
i going to open and *try* to maintain a new section, called monday tips, where i going to give you some small codes/tips that don’t worth a full post but can save you some ours of work, so here go:
1)Want to install a DNS? here is a good tutorial that will help you to quickly create a DNS.
2)A good resources of scripts for iptables i found it here
3)Why lawyers don’t run startups a good article about business.
4) copying files using SCP from console is very easy, the syntax of command is:
scp -r -P 22 user@remoteserver:/home/XXX -P 22 user@remoteserver2:/home/XXX
basically there you are copying from server 1 to server 2, if you are logged in a ssh console and want to copy files there, just replace the remoteserver2 with the directory. also, the -P defines the port number, is is 22 as default, is not needed. -r also make it recursive, it going to copy all the contents if a folder is choosed.
and wall for today, i hope you find this information useful
Regards,
Shadow.





