Today is a good day to code

iPhone onorientationchange Event

Posted: December 31st, 1969 | Author: | Filed under: iPhone, java, Programming, Uncategorized | Tags: , | 1 Comment »

iPhone onorientationchange Event

Picture of Irvin* Update: I was wrong about this, it won't work. you have to use the dirty markup method and put it in the body tag. Safari's DOM renderer processes the JavaScript in parallel, so the body element is not recognized by the JavaScript if it is at the top. There are other ways to delay the execution of this command until the DOM is rendered, but they aren't worth the trouble *

I'm glad that Apple has created this event. I think most of the developers who started early with the iUI were polling for the window.innerWidth. While this was an awesome hack before Apple had a proper event. Well now they do, but I don't really like their example code.

It is somewhat nitpicky, but applying an event handler to an element explicitly in the DOM is not the best way to do it, besides not being valid HTML / XHTML, it is sloppy. Some would argue that most validator plugins will detect your addition of the event handler post rendering anyway, but at least your html looks clean for debugging if you don't have strings of event handlers on all of your elements!

At any rate, what Apple's example shows is:

That will work as long as your JavaScript libraries are included before the body element, something that YSlow! and Yahoo! suggest hurt performance. I don't agree with Yahoo! on their performance guidelines most of the time, but in this case, I have seen the improvement, and I am now a believer.

If you leave your markup alone and add the event handler in one of your JavaScript files like:

document.getElementsByTagName(“body”)[0].onorientationchange = updateOrientation;

Then your markup will remain clean, you can move your JavaScript script include tags to just before your closing body tag, and you will have a quick and efficient page.

If you are handling the orientation event, it will return 0 for portrait, -90 for landscape after a clockwise turn or to the right, and 90 for landscape after a counter-clockwise turn, or to the left if the screen is facing you.

For most cases, you would have to determine if it was not 0, like this:

if(window.orientation != 0){
// iPhone is in landscape mode
}else{
// iPhone is in portrait mode
}

That is simplistic, but it should get you started. At any rate, I am really happy that Apple is bringing more events online, hopefully we'll get firmware 1.1.3, local storage, and advanced WebKit CSS functionality in that update, or shortly thereafter.

Apple's original article is at:

http://developer.apple.com/documentation/AppleApplications/
Reference/SafariWebContent/HandlingEvents/chapter_8_section_6.html


JavaScript and Object Oriented Programming

Posted: December 31st, 1969 | Author: | Filed under: java, Programming, Uncategorized | Tags: | No Comments »

JavaScript and Object Oriented Programming

Picture of IrvinFor some time now I have advocated using what I, for lack of a better term, call object oriented style coding when approaching JavaScript. Recently, Joseph Smarr talks about some of the challenges faced by developers who try to do too much with JavaScript. He brings up some good points, I'd like to discuss some of his findings, and add a few that were left out.

He talks about every line having a cost with JavaScript and being brief when writing it. I would go along with that, with the caveat that if you are writing something with JavaScript's condensed notation and no one including yourself will be able to understand it in a year, you should probably go ahead and write it out longhand with comments, whether you leave the comments in when you release it is up to you, but I think the extra cost in performance is worth clarity.

You have to be willing to not make an object out of everything. In lots of ways, we have to “kill our darlings” to borrow a literary term, in that you have to be willing to break encapsulation, destroy design patterns, and do some hacking for performance. It is important to remember that with languages people typically use OO for, the compiler will take out redundant statements, and optimize away some of the things you have done to maintain sane code. With scripting languages, especially JavaScript, we don't have that luxury. That elegant factory pattern that looks so good in the code may, and probably will fall apart as far as execution performance. Unfortunately, many developers will not compromise the ideals they have been taught once they reach this point. So we end up with slow performing UIs that could be fast.

One thing that Joseph Smarr doesn't talk about is taking a hard look at what really needs to be done on the client. It is critical, when profiling to determine if the speed of a round-trip to the server is worse than the performance of your code against the client. It is important to think about ways in which the beefy server can do some processing. As sexy as a fully SOA is, if it performs like a dog, no one is going to care what the technology is behind it.

Many of the best developers refuse to work with JavaScript because of these things, and it is a shame because I think these are the people who are best in a position to change some of the negative aspects of the language. Also, writing procedural code forces some efficiency while OO seems to allow just about anything. If you can't handle the prospect of writing JavaScript, try C, not C++ it is the same sort of thing.


JoostBook – Joost to Facebook Interface Widget

Posted: December 31st, 1969 | Author: | Filed under: java, JavaScript, Programming, Uncategorized | Tags: , , , | No Comments »

JoostBook – Joost to Facebook Interface Widget

Picture of IrvinSince I'm in love with Joost, I have been thinking about good applications that I could write for the platform. Before I get into talking about the widget / plugin, let me just say that the experience I have had with communicating with the Joost engineers, through their joost-dev google group, as well as them allowing early access to their SDK, has been outstanding. I have rarely come across a more open and generous group. Typically, the SDK guardians are very selfish about discussing future features, and are usually quite arrogant about the possibility of a developer finding an undiscovered bug. None of this has been the case with the Joost SDK staff.

If you don't want to read the details about how I built it, and you just want to use it, you can get it here: JoostBook: Joost / Facebook Interface. You will need Joost, and a facebook account to get started.

Now, about the widget. Firstly, the installation is a little wierd because of the level of control facebook insists on. In order to use the SDK, you have to authenticate, if an unauthenticated request is made, the response is with the facebook login page. This makes for some unique error catching conditions.

Secondly, we web developers often take for granted that the DOM will have a listener attached to it, and will automatically refresh if anything in the DOM changes. Well, I know that the Joost engineers are working on it, but it doesn't refresh, and therefore, while you can create new XHTML elements, as well as modify the ones that are there with JavaScript. You are best off currently just hardcoding all of your objects up-front, and changing their contents. Also, injecting XHTML using innerHTML doesn't really work so well currently either. I'd suspect that much of this is because there is a bridge between the 2D world of XULRunner / Mozilla, and the 3D world of the Joost interface. I'm sure there is a lot of complexity between the two.

So basically, once you have downloaded Joost, and installed the plugin, the first thing I had to do was check for if you are logged in, if you aren't logged in, it has to show you the facebook login page in an iframe so that the XULRunner browser can be cookied. After that, the widget should work like one would expect. You may have to log in alot, and if you aren't logged in, obviously the application can't update the JoostBook facebook application.

Writing the Joost plugin was the easy part, getting the facebook stuff to work was the hard part. Most of it was because the error handling is terrible. Since facebook doesn't allow you to see the 500 errors that your server is throwing, and it doesn't log it, you have to find other ways to check to see if your server is behaving properly. I spent a lot of time in my logs checking for errors.

The install process is a little wierd too, for example, in Firefox 2.0.0.8 on Windows XP, when I clicked on the Joda file linked in the page, it tried to open it as if it were some kind of markup file, obviously the joda looked like garbage, I had to right click and save. Perhaps if I had used a joost:// link it would have worked OK, but I think more research is in order. I didn't really try it in IE because most of the readers of this blog use Firefox, but it should work the same way.

Then having to install the application in facebook can be a little difficult as well. Well, the installation isn't difficult, its the concept that you have to install two applications that work together that is hard. At least there is no particular order in which you need to install them, worst case whenever you run the JoostBook plugin in Joost, it'll show you the facebook login page all the time.

At any rate, it was a fun experience, and I still think the guys at Joost are on to something. I'm slightly less psyched about the facebook platform, but I'm still excited about it.


WebKit Nightlies With Local Storage and Wild CSS Functionality

Posted: December 31st, 1969 | Author: | Filed under: java, Programming, Uncategorized | Tags: | No Comments »

WebKit Nightlies With Local Storage and Wild CSS Functionality

Picture of IrvinMy head is still spinning after downloading the WebKit nightlies and messing around with the local SQL Lite storage, and the css animation and transitions. Man, this stuff has my head swimming with the possibilities for interesting applications.

More importantly, when is this going to make its way to the iPhone. I hope we don't have to wait for Apple's next big cat to be released for us to get some of these features. I know the SQL Lite access is really early, and also that most of these features are HTML 5 things being given to us early by the webkit staff, but for early features, they sure are pretty complete looking at first glance.

I don't know what the what3g is in an uproar about, with Apple starting work on HTML 5 in their browser. To me it looks like innovation. It isn't always tidy, but it is always welcome. I haven't been this excited about the web since gmail hit and AJAX started taking over!

Anyway, please check out the nightlies at: WebKit Nightlies I am not sure if it will work with PC Safari, I think those are earlier builds, but if you have a Mac, have a field day. Also, check out Drosera, the JavaScript debugger, and the Web Inspector in the debug menu, if you are using databases, you can check out the contents with that. Boy Safari has come a long way in a short time. I think the battle will definitely be WebKit and Mozilla, I wonder what Firefox is going to come back with. The new WebKit stuff is really strong.


Coding Using Mides the PHP IDE for iPhone

Posted: December 31st, 1969 | Author: | Filed under: iPhone, java, mides, Programming, Uncategorized | Tags: , | No Comments »

Coding Using Mides the PHP IDE for iPhone

Picture of IrvinSo I have been checking out twitter, and it seems that people think that I am either crazy or sadistic for writing Mides so that one could code using PHP, JavaScript, and HTML on iPhone. The truth is that I am neither, well perhaps a little crazy.

What made me write it is that I was thinking it would be awesome if I could work on my sites on the iPhone. Everyone has downtime every once and a while where you wish you could do a little work. Mides does that for me. Working on projects here and there, it is amazing how much one can get done.

The way I came up with it was actually working in NetBeans and playing around with the code-collapse. That started the thought process that lead to the code collapsing metaphor present in Mides. I know that some hate it, and others love it, but for me it seemed to be a quick easy way to drill down into your code. Also, an additional benefit was that it allowed me to focus on the class, function, or loop that I was writing.

To me, the keyboard has never really been too much of an obstacle. I understand that for others it can be a problem, but having had my iPhone since it came out, the last time I thought about the keyboard was about two weeks after I had bought it. I really wanted a flexible bluetooth keyboard for blogging from the phone, but later I really didn't need it.

While not perfect, the code completion does help a lot, and once you get used to it writing markup can be really quick. I will keep working on ways, other than the ones already present, to allow Mides to accelerate text entry.

Some of the things that frustrate me while using it, even though I wrote it, are that I can't easily look up reference material on code that I am writing without leaving the code. Clearly copy and paste would be nice, and I wouldn't be surprised to see it in a subsequent release. The ability to debug JavaScript, and a real HTTP server running locally would be awesome, to help with resolving absolute paths. Ideally, I'd be able to process PHP directly on the iPhone, but that is complicated for a number of reasons.

Many of these features will be coming, but actually most of the effort has gone into thinking about how to achieve the complexity of a full modern IDE, while keeping the UI cluttered. I feel that I have succeeded in many places, where others need work. It is my aim, however insane, to build Mides into as feature complete an IDE as is possible given the hardware and software stack provided by Apple.

I used Mides to build the mides site, including all of the canvas tag work. I think I only did about 5% of the coding on the Mac. There were some interesting incidents working with alpha quality code trying to build a site on the iPhone.

Well, feedback is always welcome, and I am not above listening to rational criticism. I want Mides to be useful to everyone, and am looking forward to the day when I can do just about all of my work from my phone!