Davide Cassenti

Forge Titanium

Ideas and samples made with Appcelerator Titanium

Debug messages improved

March 21st, 2013

A few weeks ago, I’ve been playing with some app that was running a slow; however, it was not so easy to understand which part of the code exactly was causing the problem. I decided to print out debug messages, one every few blocks of code, calculating how much time each of those blocks was taking. The first code I uses looked like this:

This code works fine if you print out just a few messages, and if you find the core of the problem within 10 minutes; afterwards, you will get mad understanding what’s going on. I know, you can just print out different messages, instead of the time only, but that means to copy, paste, change, search etc. To avoid all that, I decided to implement a module, called TiTimeLogger. This module would apply the above idea, adding some additional feature such as the line number. Continue reading

Javascript scope: function vs var

March 17th, 2013

Javascript can give you an headache sometimes; the way the scope works in this language is something that definitely will.

Let’s see an example:

The code is pretty simple: we are defining a global variable called x, and then re-defining it inside the test function; you would expect the first time we call console.log(x) to see “hello” printed out in the console, but that’s not the case. This is because, since a local version of x is defined inside test(), Javascript will use it inside the whole function, even before it is defined; however, since the actual value is set after the first call, the first call will simply print out undefined.

There is a more subdle problem here: function declaration do not follow this path. Here’s another example:

In this case, we are defining two functions inside test(): one is declared – as before – using var; as we have seen before, when we call bad(), the variable is undefined, and the call will fail. That should be pretty clear. However, the same is not happening for the other function: despite we declare good() after we call it, doing this without the var keyword will not cause the mentioned problem. Which means, you will see This will work in the console, and an error message afterwards.

This issue might come up in particular when you minimize the code, before using it: you might have used the good() format to declare your function after you use them, and everything works, but once the code is minimized, your app will stop working; this happens if the minimization converts your function from the original format to the other one. That said, always pay attention how your variables (and functions) are declared in your code: despite you are free to do as you prefer, it is always better to declare everything in the beginning of the function, in order to avoid problem.

Map clustering

October 31st, 2012
Map clustering

Map clustering

The idea for this proof-of-concept module came from a request I got a few weeks ago form one of our customers; the map clustering idea is simple: merge 2 or more annotations on a map when zooming out. The original idea comes from a blog post by Mika Tuupola, while the code has been simplified (and thus it is not that perfect as it should).

At any rate, here’s the idea: the module creates a native MapView, calculates the pins to add to the map and reloads it every time you zoom in or out. The module includes an example app: let’s look how it works.

Continue reading

Create a modal window with Alloy

October 27th, 2012

For those who don’t know yet, Alloy is a new Appcelerator MVC framework, which allows to simplify the creation of mobile apps. Tony Lukasavage did a very good introduction to the framework during Codestrong: if you didn’t manage to attend the presentation during the event, spend a moment to take a look at it :

Continue reading

A brand new blog

October 26th, 2012

I’ve spent the last week in California, travelling because of the Appcelerator Codestrong event. It was the second Codestrong for the company, but the very first for me, as I’ve recently joined Appcelerator in the Customer Success team. The event was amazing: 3 days of talks, meeting with developers and customers, a all-night-long hackathon (and for a good cause!) and the never missing fun.

It has been a great experience, an interesting and useful way to get in touch with the community of developers and companies who decided to use Titanium to build mobile apps. If you missed it, be sure to check out TiConf: it’s a community-organized, 2-days event that will be held in Valencia next February! Looking forward to be there – and to eat some real paella, of course!

However, after I met all these great developers and people at the event, I decided to open this new blog: I feel like lame not to share what I do everyday: sample codes, ideas, news and everything that comes to my mind related to Appcelerator and mobile development in general. If you want to know more about my job, check out my website; all my sample codes will instead be on my github account.

Code strong!