Tag Archives: ajax
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,
weeks ago i posted this function that basically let you serialize object and arrays in javascript and is compatible with the unserialize funcion in php. recently i discovered a problem in this function related with the management of the weird, other language characters. the problem is that when you save a word with, let say, russian characters, the count of the chars is different as how php do it, so when you try to unserialize the string you get a offset errors. to solve this, you need to convert the string into unicode, serialize it and then send it to the php.
the function to convert any string into unicode with javascript is the following:
function convertToEntities(tstr) {
var bstr = '';
for(i=0; i127)
{
bstr += '&#' + tstr.charCodeAt(i) + ';';
}
else
{
bstr += tstr.charAt(i);
}
}
return bstr;
}
and in the php, you process the string like this:
$unserliazed= html_entityt_decode(unserialize($string),ENT_NOQUOTES, "UTF-8");
is very important that you set the UTF-8 charset to the decode function, so you get the original string.
also, remember that the database need to be support unicode-utf8, here you will find a quick guide for it
Regards,
Shadow.
Related Post
Hi,
i’m still with the images stuff
now i needed to integrate a crop image script using js. so i started to look for a premade solution. you can find several of them here, here and here. i was looking for a mootools based solution and i found 4. for my surprise,one didn’t exist anymore, one of them didn’t worked with mootools 1.2, the others was too complicated and not flexible and none offer what i really needed, that was able to offer “mirrors” as preview of the crop in 2 different sizes. in my adventure, i tried to fix the first one (MooCrop) porting it to 1.2 but i leave it why it would take me too much work, then i tried to modify cwCrop but the code structure seems to be less flexible, and finally i decided to go with uvumiCrop, as it already have a mirror function and just need to be converted into a multi-div function.
so for those who want the mirror feature in the uvumiCrop script, here are the functions and code you need to replace:
Add this to the class var declaration
previewImageArray:new Array(),
On some place around line 102, paste this
this.previewImageArray = new Array();
replace with this from lines 285 to 354
if(this.options.preview){
if(isArray(this.options.preview)){
var previewImgObjArray=new Array();
this.options.preview.each(function (item,index){
var initWidth= $(item).getStyle('width');
var initHeight= $(item).getStyle('height');
//we put the preview in a wrapper div with a fixed height, because preview height and width may change.
var previewWrapperObj = new Element('div',{
styles:{
height:initHeight
}
}).wraps($(item));
//Setting the preview container
$(item).setStyles({
display:'block',
position:'relative',
width:initWidth,
height:initHeight,
overflow:'hidden',
margin:'auto',
fontSize:0,
lineHeight:0,
opacity:1,
visibility: 'visible'
});
//cloning the original image for the preview
var previewImageObJ = this.target.clone();
previewImageObJ.set('id','imgObj_'+index);
previewImageObJ.removeProperties('width','height').setStyle('position','absolute').inject($(item));
previewImgObjArray.push(new Array('imgObj_'+index,item,initWidth,initHeight));
}.bind(this));
this.previewImageArray=previewImgObjArray;
}else{
if($(this.options.preview)){
this.preview = $(this.options.preview);
//we put the preview in a wrapper div with a fixed height, because preview height and width may change.
this.previewWrapper = new Element('div',{
styles:{
height:this.previewSize.y+2*this.preview.getStyle('border-width').toInt()+this.preview.getStyle('margin-top').toInt()+this.preview.getStyle('margin-bottom').toInt()
}
}).wraps(this.preview);
}else{
var previewWrapper = new Element('div',{
'class':'preview'
}).inject(this.toolBox,'top');
this.preview = new Element('div').inject(previewWrapper)
}
//Setting the preview container
this.preview.setStyles({
display:'block',
position:'relative',
width:this.previewSize.x,
height:this.previewSize.y,
overflow:'hidden',
margin:'auto',
fontSize:0,
lineHeight:0,
opacity:0
});
//cloning the original image for the preview
this.previewImage = this.target.clone();
this.previewImage.removeProperties('width','height').setStyle('position','absolute').inject(this.preview);
// this.addEvent('onPreview',this.updatePreview.bind(this));
}
this.addEvent('onPreview',this.updatePreview.bind(this));
}
replace updatePreview function with this code below:
//Function to update the preview
updatePreview: function(top,left,width,height){
if(this.previewImageArray.length>0){
this.previewImageArray.each(function (item,index){
if(height*item[2].toInt()/width<item[3]){
$(item[1]).setStyles({
width:item[2],
height:(item[3].toInt()*height/width).toInt()
});
$(item[0]).setStyles({
width:(this.target_coord.width*item[2].toInt()*this.scaleRatio/width).toInt(),
height:'auto',
top:-(top*item[2].toInt()/width).toInt(),
left:-(left*item[2].toInt()/width).toInt()
});
}else{
$(item[1]).setStyles({
height:item[3],
width:item[3]
});
$(item[0]).setStyles({
height:(this.target_coord.height*item[3].toInt()*this.scaleRatio/height).toInt(),
width:'auto',
top:-(top*item[3].toInt()/height).toInt(),
left:-(left*item[3].toInt()/height).toInt()
});
}
}.bind(this));
}else{
if(height*this.previewSize.x/width<this.previewSize.y){
this.preview.setStyles({
width:this.previewSize.x,
height:(this.previewSize.x*height/width).toInt()
});
this.previewImage.setStyles({
width:(this.target_coord.width*this.previewSize.x*this.scaleRatio/width).toInt(),
height:'auto',
top:-(top*this.previewSize.x/width).toInt(),
left:-(left*this.previewSize.x/width).toInt()
});
}else{
this.preview.setStyles({
height:this.previewSize.y,
width:(this.previewSize.y*width/height).toInt()
});
this.previewImage.setStyles({
height:(this.target_coord.height*this.previewSize.y*this.scaleRatio/height).toInt(),
width:'auto',
top:-(top*this.previewSize.y/height).toInt(),
left:-(left*this.previewSize.y/height).toInt()
});
}
}
},
and the isArray() function used at the beginning of the code:
function isArray(obj) {
return obj.constructor == Array;
}
so, now in the initialization of the uvumiCrop object, you set the preview property as a array ( new Array(‘id1′,’id2′) ) with the id’s of the divs that will be used as mirrors. is important that you set the width and height of each div before you create the uvumiCrop object. this code will maintain the original behavior of the original script, you can still have the floating mirror and the unique mirror.
is also important to mention that i changed the code so the container don’t get the width and height deformed and work as a container hiding the rest of the image. is done like this so the user can know really how it will look after the crop process.
i have sent this post to the developer, so maybe he include it on the next release
i hope you find this useful and save you some time
Regards,
Shadow.
Related Post
Hi,
recently building a app i was need to suggest data to the user when trying to fill a input. so i look for a js script for this. i usually use mootools for my development in js, so looking for a plugin i found this: Meio.Autocomplete plugin for mootools
is a really nice plugin, between their features, support the inbuilt ajax calls using the Request.JSON method from the mootools framework. also support to set up filter for the returned data, the set of field parameter to look for the input by the user, the minimum of characters that the user need to enter to start looking for it, delay between the ajax call and the search in the data and many other properties. you can also customize the look and feel of how the options are displayed.
from their web page you can download the source code and the compressed version for production environments. also there you can see plenty of documentation explaining each property of the plugin and several examples to see and try online.
i hope it help you in some way
Regards,
Shadow.
Related Post
Hi,
the last days i was working on a API for project. for that, i was needed to set a OAuth system. all went perfect but i have a problem when i wanted to use the API in a AJAX app.
as you already know, AJAX it’s a technique where you use javascript to request info to a page and show it to the user. as you also know, javascript it’s executed in the user machine, and for OAuth it’s a problem :s
no matter what how it’s implemented the OAuth system in the API that you need to use, you need always to send the $consumer_key and the $consumer_secret encrypted in the signature, also depending of the type of request, also need to send the $token and $token_secret, for that, you can’t include in any javascript way (or in a swf) any of this vars why compromise the security of the consumer, why any with this data can access to the protected resources as you.
for that, the solution it’s to create a bridge between your AJAX/FLASH app and the API/OAuth server.
the process it’s simple, create a php with a code like this:
<?php
//check if the one who are making the call to the bridge it’s trusted
//for that you can use a filter ip, an authenticated session or any method that fits your needs
$consumer_key=”lalala”;
if($trusted){
//making the request
$result=$OAuth->request($url_of_the_server.$_GET['service'],
$consumer_key,$consumer_secret,$_SESSION['token'],$_SESSION['token_secret']);
echo $result;
}
?>
of course the code can be diferent depending of your client library and stuff, but basically that are the steps to make the bridge:
1)check that the one who call the bridge it’s trusted or ahiterized, checking a sesssion var or anything what you have
2)make the request using the $service var that define the service to call to the service
3)print the result of the call and it’s done
i hope this give you some help at the time to use OAuth and REST with ajax and flash
Regards,
Shadow.





