Oct 31 2009

JQuery Part 3 – Intro to Functions

This is part 3 in a series of JQuery tutorials. I strongly recommend at least reading Part 1 before continuing with this tutorial. Part 2 is optional but would also be a great background before we get started. As for this tutorial, I will be going over creating an executing functions to make your code more efficient.

Often when writing JQuery code(especially for plugins) there are times when you need to execute the same block of code multiple times. Instead of writing them all out individually and making our code really terrible process wise, we can use functions and function calls to call the same block of code over and over.

Creating Functions

You can create a function in 2 different places. The first is in the same script tags, but not inside the document ready function. Or second, you could create a brand new script file and then reference that script file in your header. Inside this script file you could create your method and it would be able to be accessed by the main file.

To create a function, the syntax looks like this…

function nameOfFunction()
{
	//your code here
}

This tells the browser that there is a function called nameOfFunction that is not being passed any parameters. Passing parameters is something I’ll get into a little bit later. Now say you wanted to alert the user with a javascript pop-up box every time this function was run. You could add some code into the function to allow a dialog box to appear like so.

function nameOfFunction()
{
	alert("We are now alerting the user!");
}

Executing Functions

Now that we have a function created, we need to somehow call that function to execute. That’s where our javascript method calls come in. Let’s say we wanted to execute the previous function. The code would look like this.

$(document).ready(function(){
	nameOfFunction();
});

Of course this would call the function immediately after the page was ready. You can use a function call in JQuery in any function. So if you wanted to add it inside of a .click function you could do that as well. That’s all you need to do to execute functions in JQuery.

Passing Arguments to Functions

Let’s say you wanted a function to work with a piece of data from a form, but you didn’t want the function to retrieve that piece of data inside the function. You can use a piece of code called an argument to pass to the function. This allows you to pass values to a function so they can be used. To create a function with an argument then all you need to do is add the argument into the brackets in the function header. It looks like this.

function nameOfFunction(argument)
{
	//your code here
}

Now obviously since we have that argument in the argument ready to do something with it, we may also want to return a value back to the original function so that we can maybe display the result of a calculation or something like that. If we want to return a value all we have to do is insert a return value into our function.

function nameOfFunction(argument)
{
	var total = 10 + argument;
 
	return total;
}

This function will now accept a value, add 10 to that value and return it to the original function call. This way if we need to manipulate some data quite a few times, we could use a function to do that display the result.

Function Calls with Arguments

Now that we have a function made that’s accepting an argument and returning a value, we can use a function call and pass an argument to the function so it can do the calculation and we can then display the result. The function call looks something like this.

$(document).ready(function(){
	alert("The total is = " + nameOfFunction(10);
});

In our JQuery ready function we are now just displaying an alert. Inside the alert we are actually making the function call and passing a value of 10 to our function. The function is then going to add 10 to our passed argument and return a value of 20. The alert therefore is going to display – “The total is = 20″. And that’s all for basic functions and function calls.


Jul 3 2009

13 must have apps for Mac OS X developers

1. Apple Developer Tools – These are just a no-brainer here. There is a huge arsenal of apps that come standard with every mac that absolutely EVERY developer that is developing for mac os x needs. Xcode, Dashcode, Instruments, Interface Builder, etc. There are a ton more to check out if you haven’t already, and all of them will considerably ease your development experience on OS X.

2. Eclipse- Eclipse is the ideal application for java development on Mac OS X. With the amount of features that eclipse offers, it’s almost impossible to skip this application if you do any level of java development. The “intellisense” that eclipse includes while you’re coding is unprecedented.

3. Photoshop/ Gimp – This is a must have for any type of website development/interface development. Whether it’s doing website mockups or drawing new buttons for your application, this is a must have.

4. Coda – I can’t speak enough good things about coda. This application has exceeded my expectations in every way possible. Whether it’s through the handy “sites” feature, or connecting to a server using the built in console, this is by far the best web development app for Mac OS X. Well worth the 100 bucks!

5.uTorrent – A very simple torrent application. Extremely easy to use and very efficient. Having the ability to select which files you want to download and which you don’t is a huge bonus.

6. Adium – A very powerful messenger application for Mac OS X. From IRC plugins, to customizing the look and feel, Adium can pretty much do it all.

7. Jing – This screen capture application hides almost out of sight in your menu bar but packs some powerful features. Jing has the ability to capture screen casts of your applications in action as well as screen shots. Great App!

8. QT Creator – From the designer’s of the famous UI toolkit QT comes their first IDE. QT Creator is a fantastic IDE for developing QT Apps. From the WYSIWYG editor to the syntax editor, great app all around.

9. Prism – Pretty cool new(ish) app from Mozilla. This application allows you to capture sites like gmail and make them into a desktop application so you don’t have to open a browser every time you want to say check your email.

10. iStat Menu’s – This light-weight app will allow you to truly track your applications performance and see if you potentially have a memory leak in your app. Has a separate monitor for each CPU to allow the user to see what’s going on in each CPU at any given time. A must have even for non-developers.

11. XMind – This extremely powerful mind-mapping tool is great for those pre-alpha stages of development. Getting as many ideas down as quick as possible is extremely easy for this application to handle. The application is based on the eclipse platform and is very solid.

12. Virtual Box – This open source application is great for testing applications in numerous environments. Test run your apps on Linux, Windows, BSD and more.

13. VmWare Fusion – This is a must have for all developers but especially for those doing web development with coda. Allows you to run windows applications natively on Mac OS X. Another great app for testing in multiple environments as well.

Cheers


Apr 4 2009

Microsoft and Open Source

As some of you may know I am an open source advocate. The ability to modify and change the source code of your application to make it work the way you want to is a huge plus. With Microsoft being one of many companies that keeps it’s doors closed when it comes to it’s source code, it makes it extremely difficult to maintain a trustworthy relationship between customers and developers.

Recently however, Microsoft has released the ASP.NET source code. The article found here outlines the details of the release.

I do think that a bigger step is needed to fully gain the acceptance of the open source community. However i do think it is a major step in the right direction. I would like to see another major jump for other companies to go the open source route. Customers having the ability to review the source code to make sure there are not “hooks” inside the code. I think by doing this, customers of Microsoft will have eventually begin to regain the company’s trust. But, I’m not sure it will be in time.

Cheers


Mar 16 2009

Creating an Application Specific Console in QT Jambi

I am currently working on a project that required a console with a very specific purpose. It needed to communicate with a custom back-end protocol for use in a client/server environment. This sort of specific tutorial is not surprisingly unavailable online. The first step when setting up an application specific console is actually setting up the back-end. This is something that will be potentially done as a future tutorial. Anyways back to building the console window. The components included in the console window will be one QTextEdit, and one QLineEdit. This can be constructed in any way you like. The QTextEdit can be on the bottom and the QLineEdit on top or the other way around, that’s up to you.

Next, we will need to be adding a custom action listener to the QLineEdit so that when the enter key is pressed inside the QLineEdit the command will be sent to the server and then displayed in the QTextEdit window. As a future feature implementation, syntax highlighting could be implemented for an easier display inside the QTextEdit window. The custom action listener to add to the QLineEdit bar looks like this…

1
QLineEdit.returnPressed.connect(this, "NameOfActionListenerMethod()");

This will ensure that everytime the enter button is pressed, the string that the user entered will be able to be transfered to the server, or wherever else in the program you would like it to go. Now for the action listener method. The method should be declared as public with a return type of void.

1
2
3
4
5
public void terminalWindowEnterPressed()
    {
        terminalWindow.append(enterArea.text());
        enterArea.setText("Protocol Name > ");
    }

Then once you have the action listener built to just send the data to the window, you can do the same with sending it across a network.

All Together the code looks like this. All you have to do is copy/paste the code into your program and use it at will.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
     *
     */
    public void buildTerminal()
    {
        terminalFrame = new QFrame();
        terminalFrame.setWindowTitle("Window Title");
 
        terminalWindow = new QTextEdit();
        terminalWindow.setReadOnly(true);
 
        enterArea = new QLineEdit();
        enterArea.setText("Protocol Name > ");
        enterLabel = new QLabel("Enter Command");
 
        terminalFrame.setMinimumSize(600, 400);
 
        enterArea.returnPressed.connect(this, "terminalWindowEnterPressed()");
 
        QGridLayout layout = new QGridLayout();
        layout.addWidget(terminalWindow,0,0,10,10);
        layout.addWidget(enterArea,11,1,1,9);
        layout.addWidget(enterLabel, 11,0,1,1);
 
        terminalFrame.setLayout(layout);
        terminalFrame.show();
    }
 
    /**
     *
     */
    public void terminalWindowEnterPressed()
    {
        terminalWindow.append(enterArea.text());
        enterArea.setText("Protocol Name > ");
    }

Cheers