Category Archives: javascript

angular2 – ‘this’ binding context lost in .catch call

I noticed that “this” was undefined when calling .catch method: return this.http .get(‘/api/misc-url’) .toPromise() .then(response => response.json()) .catch(this.handleError); It happens that this is happening because of the way function is called in .catch. By providing directly the method, we are losing context. By wrapping call into arrow function we can restore it! (better than .bind(this) :)) return this.http .get(‘/api/misc-url’) .toPromise() .then(response => response.json())         .catch(error => {this.handleError(error); }); Easy 🙂 sources http://stackoverflow.com/questions/36227996/angular2-getting-confused-with-observable-catch-closure-scope  

javascript: suffle array (one-line script)

Here is a fantastic way to shuffle an array in javascript: myArray.sort(function() {return 0.5 – Math.random()}); So simple yet so efficient, I love it sources http://css-tricks.com/snippets/javascript/shuffle-array/

jquery plugins: popup message

Here are some great jquery plugins for showing up popup messages: http://akquinet.github.com/jquery-toastmessage-plugin/ http://needim.github.com/noty/  

google script equivalent of xlDown, xlRight

I recently started creating scripts for google spreadsheet documents, the javascript based scripts. Unfortunately, I could not find the equivalent of xls vba range(“A1”).end(xlDown) in native google script functions. As such I created a working js equivalent. Only issue with it is performance. It is not really fast, but this is more due to Apps Script rather than the code itself or the fact that it is written in js. In case someone find a native equivalent to this function, please do not hesitate to share. /** * V0.2 => PERF IMPROVED * * Home-made equivalent of xls range().end(xlToRight) function […]

Create a custom dojo build

When you develop an application with dojo, you can pretty quickly end-up with tens of http calls to load all dojo.require() files on demand. In my case, for a simple script relying on dijit.Dialog & dijit.Tooltip, firebug reporting more than 25 http calls for individual files. In production, this is not optimal at all. So we will try to use dojo utilities to create our custom dojo file containing all (and only) the required dojo code into a single file. Problem is that there is scarce help on this matter on the Internet and official documentation is not correct! Here […]

issues with IE when cloning file input node

I wrote a js script in which I needed to clone an <input type=”file”> node. As usual, IE spoiled the party. It appears that when you clone an <input type=”file”> node (through “node.cloneNode(true);” or “dojo.clone(node);”), the value property is not cloned. Therefore you end-up with an almost useless cloned node. For security reasons, .value property of such node is not writable with js (only readable), this part I understand fully, but why on earth can’t IE  set it on the cloned node??? FF does it pretty well… Only solution I came up with was to attach input node into a […]

cannot set a name on hidden node in IE

I tried to create an hidden input node with dojo using dojo.create(‘input’, {type:’hidden’, name:’foo’, value:’bar’}). It worked fine in FF, but when looking at IE7, created node always had no name. After multiple attempts, the only workaround I found was to create an outer node and fill it with hidden node converted as string, using .innerHTML and then retrieve it back using .firstChild property of outer node. Here is the example: var dummy = dojo.create(‘div’, {innerHTML: ‘<input type=”hidden” name=”foo” value=”bar”/>’}); var myInput = dummy.firstChild; By doing this, when myInput got inserted into DOM, it kept its name property.

how to debug/keep-an-eye on all your http traffic

While looking for a way to track xhr/http request in IE, I found out fiddler (www.fiddler2.com). This tool is simply fantastic, it allows you to keep an eye on all your http requests!!!! ALL of them. Very useful to check if a file is still being uploaded, to track xhr requests and so on… For privacy concerns, make sure to use it while debugging only (it acts as a proxy, god knows what they do with proxied data). sources http://www.fiddler2.com http://stackoverflow.com/questions/1126021/ie8-xmlhttprequest-debugging

.indexOf on arrays with IE

IE (at least version 7) has no implementation of ‘indexOf’ function on arrays. Good news is that dojo has implemented a function just for this, and guess what, it’s dojo.indexOf(). Here is the signature of the function: dojo.indexOf(/* array */haystack, /* mixed */ needle, /* integer? */fromIndex, /* boolean? */findLast); sources http://w3schools.com/jsref/jsref_obj_array.asp http://www.pearweb.com/javascript/array-index-of.html http://api.dojotoolkit.org/jsdoc/1.3/dojo.indexOf

how to set minimum Heigth in dijit.form.Textarea

[UPDATE]: rather than using below code, simply use css min-height property. Styling with ‘height’ does not work since dojo changes it dynamically, but min-height works great (too bad for IE6 users, but it’s time to update guys!). ************************************** With v.1.3.1, dojo comes with a really nice feature on textarea form. They automatically expand while user is writting. While this feature is great, it’s pittyful that for now, no option exists to prevent box resizing upon creation. Therefore your textarea looks like a single line <input type=”text”> node and user simply has no idea that he can write a lot of […]