Karl New York City, United States of America.

English American

Archive for the ‘Geekisms’ Category

Custom AJAX Wrapper for jQuery

Friday, October 16th, 2009

I write a lot of my Javascript with the help of jQuery, and one of it’s finest features is it’s extensibility and powerful internal functions. For larger sites, especially portals, AJAX can become messy if it’s not controlled, and when things get out of hand, it’s often hard to trawl through your scripts to make changes.

That’s one of the reasons why I wrote (and use almost daily) my custom AJAX wrapper for jQuery.

The idea.
Centralize one $.ajax() method, and have methods external to jQuery control the data and callback methods before firing.

This allows for a global wrapper on all before and callback methods in case you need to do things like: show loading animations, run site statistics, setup error handling, etc.

The code.
I like to use namespaces in my projects, so I will be giving you a basic outline of a namespace setup, and then how an external function can interact with the global AJAX method.

I will base the example loosely on a CRUD schema.

// Create our namespace
var Website = {};

/**
 * Outline our webservices
 * @param {String} action - AJAX action to perform
 * @param {Object} payload - Javascript object containing AJAX method properties
 */
Website.Webservice = function (action, payload) {

    switch (action){

    case 'create':
        payload.type = 'POST';
        payload.url = '/ajax/create_data.php';
        break;

    case 'read':
        payload.type = 'GET';
        payload.url = '/ajax/get_data.php';
        break;

    case 'update':
        payload.type = 'POST';
        payload.url = '/ajax/update_data.php';
        break;

    case 'delete':
        payload.type = 'POST';
        payload.url = '/ajax/delete_data.php';
        break;
    }

    // Call the global AJAX method
    Website.AJAX(payload);

}

/**
 * Fires off the AJAX object with user defined payload information.
 * @param {Object} payload	- AJAX data options to bind to the jQuery object
 */
Website.WebService.AJAX = function (payload) {

    // If dataType wasn't specified in the payload, default to 'html'
    var dataType = (payload.dataType !== undefined) ? payload.dataType : 'html';

    // jQuery AJAX object
    $.ajax({

        // Normal properties
        type: payload.type,
        url: payload.url,
        data: payload.data,
        dataType: dataType,

        // Global beforeSend wrapper with user defined function
        beforeSend: function () {

            // Do global here
            if (typeof payload.beforeSend === 'function'){
                payload.beforeSend();
            }

        },

        // Global success wrapper with user defined function
        success: function (data) {

            // Do global here
            if (typeof payload.success === 'function') {
                payload.success(data);
            }

        }
    });

};

Now that I have set up our basic framework, I can fire off any AJAX request with minimal code, and abstract any intense data mining functions and other methods from our AJAX process. This allows for clean and controllable code.

/**
 * Some function or event that fires off AJAX
 * @param {Object} data - Javascript object containing user data
 */
var updateUserData = function (data) {

    // Create the AJAX property object
    var bundle = {

        data: {
            'user_id': data.user_id,
            'user_email': data.user_email,
            'user_firstname': data.user_firstname,
            'user_lastname': data.user_lastname
        },

        beforeSend: showSpinningLoader,

        // Success callback function
        success: onRefreshSuccess

    };

    // Fire the bundle off
    Website.WebService('update', bundle);

};

And there you have it. Simple and easy to use. Now you can trigger AJAX from anywhere in your application with all processes centralized.

Why I’ve been quiet here…

Tuesday, September 8th, 2009

So I’ve been really (really) busy at work these past few weeks. After getting back from holidays, I hit the ground running to work on the next installment of the Systembolaget AlkoholProfilen.se.

It’s a website where we can determine the drinking habits, health risks and advice to give to Swedes who take the 14 question survey. I wrote a fair chunk of this website, including some pretty neat Javascript components that we will be discussing over on Kontain in the next week or so.

So yes, lots to talk about, but I’m sure you’ve already caught up with my happenings on Twitter or Facebook. I will do my best to keep this blog updated whenever I can.

Nice!

Me @ Systembolaget

Hostgator Support

Friday, May 8th, 2009

My chat with Hostgator Support.

Chat Start Date: May 6 2009 4:07:52 PM

(4:12:14 PM) Taufique No: has entered the chat.
(4:12:22 PM) Taufique No: Hello, welcome to HostGator Live Chat.
(4:12:25 PM) karl: Hello, my domain has no hard drive space left. I get this every once in a while.
(4:12:25 PM) karl: Warning: Unknown(): write failed: No space left on device (28) in Unknown on line 0
(4:12:49 PM) Taufique No: One moment
(4:14:07 PM) Taufique No: Can I have your cPanel password?
(4:14:27 PM) karl: xxxxxxxxxxxxxxxxxx
(4:15:23 PM) Taufique No: One moment
(4:17:14 PM) Taufique No: The password doesn’t seems to be working
(4:18:04 PM) karl: Can you please fix my hard drive space
(4:19:26 PM) Taufique No: Can I have your cPanel password please?
(4:19:31 PM) karl: I gave it to you
(4:19:34 PM) Taufique No: The password your provided doesn’t seems to be working
(4:19:38 PM) karl: It works
(4:19:40 PM) karl: I just logged in
(4:20:46 PM) karl: Main Domain niceproduce.com Home Directory /home/xxxxxx Last login from 213.xxx.xxx.xxx Disk Space Usage xxxx.xx/999999 MB
(4:21:31 PM) Taufique No: Let me check
(4:22:00 PM) Taufique No: The password isn’t working for me. Can you reset the password?
(4:22:05 PM) karl: No
(4:22:10 PM) Taufique No: Or try logging out and try to log back in
(4:22:12 PM) karl: I require it to be consistant
(4:22:15 PM) karl: I just did
(4:22:17 PM) karl: it works fine
(4:24:14 PM) Taufique No: The password still doesn’t work. I won’t be able to login to your account to fix it until I have the correct password
(4:24:29 PM) karl: Then I will have to speak to somebody else.
(4:25:04 PM) karl: I’m on a shared host, and obviously the disk is full
(4:25:40 PM) Taufique No: The disk isn’t full, or else we would be getting complaints from other clients as well from the same server
(4:25:58 PM) karl: Warning: Unknown(): write failed: No space left on device (28) in Unknown on line 0
(4:26:06 PM) karl: No space left on device.
(4:26:27 PM) karl: Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
(4:26:50 PM) karl: Is /tmp full or moved or has had it’s permissions revoked?
(4:27:07 PM) Taufique No: The password is incorrect, before I make any changes, could you verify the last four digits of your CC?
(4:29:12 PM) karl: The password is NOT incorrect
(4:29:16 PM) karl: I just typed it in
(4:29:19 PM) karl: It works 100%
(4:29:28 PM) Taufique No: I can’t login Mr Karl
(4:29:36 PM) karl: What username are you using?
(4:29:58 PM) Taufique No: And I would need the last four digits of your CC anyways before making any changes on your account
(4:30:34 PM) karl: The password has been the same for over 5 years
(4:31:34 PM) Taufique No: That’s correct. I’ve been trying the same username and password you provided
(4:31:54 PM) karl: I didn’t change anything.
(4:32:04 PM) karl: You must have mis-typed it.
(4:32:39 PM) Taufique No: I’m copying and pasting it
(4:39:58 PM) karl: Is it fixed?
(4:40:45 PM) Taufique No: I’m waiting for your response
(4:40:53 PM) Taufique No: The password is not working Mr. Karl
(4:41:08 PM) Taufique No: And to fix the issue. I would need the last four digits of your CC number
(4:43:05 PM) karl: (4:31:34 PM) Taufique No: That’s correct. ?
(4:43:08 PM) karl: It just worked for me
(4:43:10 PM) karl: I don’t understand
(4:43:38 PM) karl: Try to type it in and not copy and paste
(4:43:43 PM) karl: Maybe you are copying some white space
(4:43:54 PM) Taufique No: I said, that’s correct (the information, I’ve been trying)
(4:47:30 PM) karl: Did typing it in work?
(4:49:23 PM) Taufique No: Where exaclty do you see the error message?
(4:49:31 PM) karl: On my website
(4:49:38 PM) karl: http://niceproduce.com
(4:49:40 PM) karl: At the bottom
(4:49:41 PM) karl: Any page
(4:49:45 PM) karl: I’ve seen it before
(4:49:50 PM) Taufique No: It’s working for me
(4:49:52 PM) karl: I’ve had tech support help me before
(4:50:05 PM) Taufique No: I see it now
(4:54:35 PM) Taufique No: I’m working on it
(4:54:37 PM) Taufique No: One moment
(5:08:00 PM) Taufique No: It should be working shorlty
(5:10:18 PM) karl: OK, looks fine now
(5:10:55 PM) Taufique No: Perfect!
(5:11:02 PM) Taufique No: Is there anything else I can help you with today?
(5:11:08 PM) karl: No, thanks
(5:11:20 PM) Taufique No: No problem

EA.com launches into the webosphere!

Friday, March 20th, 2009


www.ea.com by Fantasy Interactive NY / STO

So this is why my blog posts have been few and far between. We’ve been busy. I have spent the majority of my time since leaving Australia in Stockholm working on the DHTML / Javascript components with work mate David Lindvkist, and I spent a fast and furious month in New York working with the FI NYC team on the core of the front end, and now it’s live.

I got back from NYC and went straight back to work, in fact, like my other workmates (who in their 30th hour of straight work), I have spent more time in this office than out of it since returning. I worked all weekend, and until the early hours of the mornings as we in Stockholm were collaborating with EA LA and FI NYC, I am tired, but it’s worth every minute, and we’re very happy with this launch.

Unfortunately, I can’t get into too much site specific details just yet, however this project has been a great introduction to the life and work style of Fi and I’m looking forward to bigger things in coming months.

How long till Christmas?

Wednesday, January 7th, 2009

Looks like the new Macbook Pro’s are set to be the new hot thing. I sure as hell wouldn’t mind one.

Check it:

Apple today unveiled the new 17-inch MacBook Pro featuring a durable and beautiful precision aluminum unibody enclosure, and a revolutionary new built-in battery that delivers up to eight hours of use and up to 1,000 recharges for more than three times the lifespan of conventional notebook batteries. The new 17-inch MacBook Pro has a high resolution LED-backlit display and the same large glass Multi-Touch™ trackpad introduced with the new MacBook family in October. In addition, the new 17-inch MacBook Pro includes state of the art NVIDIA graphics and the latest generation Intel Core 2 Duo mobile processors. As part of the industry’s greenest notebook family, the new 17-inch MacBook Pro is made of highly recyclable materials, meets stringent energy efficiency standards and is made without many of the harmful toxins found in other computers.

“We’ve developed new battery technology that is better for the user and better for the environment,” said Steve Jobs, Apple’s CEO. “Apple’s advanced chemistry and innovative technology deliver up to eight hours of use on a full charge cycle and up to 1,000 recharges.”

Measuring just 0.98-inches thin and weighing 6.6 pounds, the 17-inch MacBook Pro is the world’s thinnest and lightest 17-inch notebook. The 17-inch MacBook Pro is the most powerful Mac® notebook yet with the latest Intel Core 2 Duo processors available up to 2.93 GHz, up to 8GB DDR3 main memory and a graphics architecture that allows users to switch between the NVIDIA GeForce 9400M integrated graphics processor for better battery life and the powerful NVIDIA GeForce 9600M GT discrete graphics processor for higher performance. The new 17-inch MacBook Pro includes a 320GB 5400 rpm hard drive standard with a 320GB 7200 rpm hard drive and 128GB and 256GB solid state drives as options. As with the rest of the new MacBook family, the 17-inch MacBook Pro includes a next generation, industry-standard Mini DisplayPort to connect with the new Apple LED Cinema Display featuring a 24-inch LED-backlit widescreen display with a built-in iSight® video camera, mic and speakers.

Read more on iClarified.

Yummy.

Berlin and a Happy New Year.

Monday, January 5th, 2009

Happy New Year.

Fi & I had an awesome time in Berlin. What an amazing city, obviously it’s drenched in history, however the most eye catching details are on the trains, in the subways, on the street, in the parks and in empty lots. Graffiti and street art rules this town. It’s the most bombed city I’ve been too. I mean everything is totally trashed, but at the same time it makes the city so unique. It’s finally finding it’s voice and it’s letting the world know. So much creativity is now reaching the outside world, or maybe we’re just now able to peer inside.

Berlin looks rough. Litter lines the streets, everything is tagged or vandalised in some way, however there is no threat of violence and we never felt unsafe. We met a woman on the flight from Stockholm who said she feels safer walking through parks at night in Berlin than she does in Stockholm.

We couldn’t have picked a crazier time to be in Berlin. NYE went for 3 days. It started on NYE with people setting off fireworks and firecrackers non-stop until the wee hours of the 2nd of January. As you can see above, we joined the party. The youth are rebels at heart, and the older generation are grumpy which makes for an interesting mix as the youth are running the town, and the oldies are just putting up with all the chaos.

What’s interesting to note is that we didn’t see any authority for the entire stay. No police, no met-cops, nothing. In fact, you don’t even need to swipe a met card to get into the subway, you just walk in and walk out. Apparently met-cops bust people constantly, however we saw none of that, and the trains run all night. We made it across the city at 7am with no problems what so ever.

Anyway.

I made the awesome mistake of booking a hotel on the other side of the city from the airport, but it was the only place I could find on such short notice. Seriously, it was like landing at Tullamarine and getting public transport to Frankston, just check the amazing view from our hotel window.

We saw Checkpoint Charlie, saw the wall in various places across the city, and walked, walked, walked to a point where we were both dizzy from lack of energy. We refueled on beer and Japanese. By the way, watching Japanese speak fluent German is a trip, especially when trying to order since they don’t speak English and the menu’s are in German! Tasty, though.

We caught up with a friend of a friend, who also is a stockist for NiceProduce in Berlin. If you’re ever in the city, make sure you pop into Van Liebling and say what up to Chris, also check the NiceProduce in the front window (nice!). He was kind enough to invite us over to his friends house for a NYE party. We of course were the only Anzac’s and the best english speakers at the party, so you can imagine the types of broken conversations we had. Kangaroo’s and koala’s (not koala bears damn you!). I also learned the hard way of setting off bottle rockets. Use a bottle. The New Year came around and the custom is to kiss and hug everybody in the room, a strange thing for strangers!

After getting up, we dragged ourselves back onto the streets and headed over to Mitte, a very trendy, highly fashionable and beautiful part of town. We found a lovely pub which used to be a laundry, a coffin maker and a bakery. It’s now one hell of a good find for some hungover tourists. Bratwurst, parma, stout and house red. It was snowing outside, and we were toasty inside. Amazing. Fi even let me break my stout-drought, as apparently my stout-pout has dispersed since moving to Hugo land. Check the hair. It’s gotta go.

We got back to Stockholm to find it well into the minuses and snowing, a lovely omen for us as it snowed when Fi came and it was snowing when she left. It’s a lovely reminder from now.

My friend Gustav is a waiter at a popular restaurant called Grill just off Rådmansgatan. I had the first steak since living Australia and Fi had an amazing seafood skewer topped with all her favourites. We drank a bottle of Chile’s finest and several cocktails which later led to midnight snow angel making, good times!

Now for a little bit of geekism…


Click the image to view a little larger…

This is Schlesisches Tor U-Bahn in Berlin. I wanted to try out the merge photos as a panorama in Lightroom, and I must say it’s pretty damn good. I’ll be out trying some 16mm shots around Stockholm soon to try to flex it’s muscles a little harder.

So that was NY in Berlin in a nutshell. I could type all night but it’s always too much to take in at once. Perhaps I’ll dribble more over a cold beer back in Australia some time, until then I’m sure Fi will be updating her blog with her version of the trip soon.

Work starts tomorrow, will be good to get back into the swing of things. Speak soon.

When there’s a will, there’s a way.

Saturday, December 27th, 2008

I managed to watch the first hour or so of the Boxing Day Test. I saw Hayden fall for 8 which was a bloody disappointment, but apart from poor form, I was having a ball watching the game from the comfort of my couch in Stockholm. I just wish there were Mac alternatives.

In other news, my post about Fi being in town, made it to the front page of Kontain.com. I love that photo.

I’ve been mapping.

Wednesday, December 10th, 2008

One of my projects this week has been to work closely with the Google Maps API. I can’t go into to much else detail though I am really happy with the results… however this is not the point of this post.

In showing my colleagues what we (me and the NY team) have been accomplishing, my attention was drawn to other developments in mapping services around Sweden. Services which make Google Maps look like Simcity 2000.

Check out Stockholm brought to you by Eniro. Here’s my apartment (second house north in the pink), and this is where I work. The links I have provided are in “Helicopter” mode. Helicopter mode comes in 4 angles (N/S/E/W) and gives you a sort of 3D view of the city. Great for a new citizen like me as it helps me get a good idea of perspective. I highly recommend using the navigation panel at the bottom left of the screen and zooming out as far as you can to get a good view of Stockholm.

Hitta.se provides a map of Stockholm also, however it uses technology which apparently can map the dimensions of buildings (height and width) and then automatically generate a 3D model of the city using satellite imagery. Definitely worth a look (just make sure you click “Stockholm” at the top of the map”.

These 2 examples make my hard work seem like a game of Connect 4. :(

Here’s to the future!

I will now sum up my week with 1 animated gif.

Saturday, December 6th, 2008

See you on the other side.

For the heads.

Friday, November 28th, 2008

No doubt it tastes geeky.