RESTful calls via AngularJS

I’ve been doing more with AngularJS lately, it’s becoming really popular. Here’s how to do RESTful calls with Angular:

var config = {headers:{'accept': 'application/json',
    'content-type': 'application/json;charset=UTF-8'}};
    
$http.post(url, data, config).
    success(function(response)
    {
        successHandler(response);
    }).
    error(function(response)
    {
        errorHandler(response);
    });

RESTful calls via JQuery

I created a post showing hoe to make RESTful calls with plain vanilla JavaScript and wanted to show how to do the same thing using JQuery. So here it is:

RESTService.prototype.doPost = function(url, data, successHandler, errorHandler)
{
    $.ajax(
    {
	data: JSON.stringify(data),
	dataType: 'json',
	type: 'POST',
	url: url,
	beforeSend: function(request)
	{
	    request.setRequestHeader('accept', 'application/json');
	    request.setRequestHeader('content-type', 'application/json;charset=UTF-8');
	},
	success: function(response)
	{
            successHandler(response);
    	},
	error: function(response)
        {
            errorHandler(response);
	}
    });
};

RESTful calls via JavaScript

I’ve been doing a lot more front-end work with JavaScript lately and have needed to make calls to RESTful APIs. One of my complaints with JavaScript is that it feels like there is not a standard way of doing things. One can always do things the plain old JavaScript way or one can use one of the many frameworks that exist. This post focuses on the plain vanilla way to use JavaScript to access a RESTful web service API. The following is a sample function that makes a POST call:

RESTService.prototype.doPost = function(url, data, successHandler, errorHandler)
{
    var http = new XMLHttpRequest();

    http.open("POST", url, true);
    http.setRequestHeader("accept", "application/json");
    http.setRequestHeader("content-type", "application/json;charset=UTF-8");

    http.onreadystatechange = function()
    {
        if(http.readyState == 4)
	{
	    if(http.status == 200)
	    {
                successHandler({});
	    }
            else
            {
                errorHandler({});
            }
	}
    };
    http.send(JSON.stringify(data));
};

To change what type of call just change the “POST” to “GET” etc.

Flash Builder attempts to launch and then fails

I wrote a post a while back about how to fix Flash Builder when it wouldn’t start. I found that there were two things to try, one of which usually fixed the problem. The first was to deactivate a product in the creative suite and reactivate it. The other one was to delete the  Adobe Flash Builder folder that is usually located in your users directory. If the deactivation and reactivation didn’t fix the problem, deleting the Adobe Flash Builder directory usually will. The frustrating part is that also removes any customization that you’ve made to your workspace which can take time to set up again. So after doing this many times I finally spent more time looking into what the problem was and I found it. Here’s all you need to do to get it to start again and not loose all of the customization. Inside of the Adobe Flash Builder folder navigate to -> .metadata -> .plugins -> org.eclipse.ui.workbench in that folder there is a file called workbench.xml. Edit this file in a text editor and remove any references to files in projects that you know weren’t open the last time that you used Flash Builder. If you don’t remember just remove all references to files. This may mean that you have to re-open some files, but that’s better than having to configure your workbench again. I was able to find which files were open by making a copy of that directory and then deleting it and opening Flash Builder. It recreates the folder and will open and you can usually see what projects are open. Another thing to try is attempt to open Flash Builder and see if it stays up long enough for you to see which projects are open. If you run into this issue I hope this helps.

AS3 Singleton

Singleton classes are very useful. So I wanted to show a simple sample of a Singleton class. I like to use these as models to store data in one location that multiple parts of your application can access. Singleton classes in ActionScript 3 for the most part are just like they are in other OOP languages. There is one minor difference. Since you can’t make a constructor private you have to add a few lines of code in the constructor. Here is a sample:

public class Singleton
{

	private static var _instance:Singleton = new Singleton();

	public function Singleton()
	{
		if(_instance != null)
		{
			throw new Error("Instantiation failed");
		}
	}

	public static function getInstance():Singleton
	{
		return _instance;
	}

}

That is my preferred way to do it for models that you know are going to be instantiated. Then you just add public variables and you have a model. Generally when I’m using models I always plan to instantiate them, but if you are not sure if you are going to instantiate the class you may want to do it like this:

public class Singleton
{

	private static var _instance:Singleton;

	public function Singleton()
	{
		if(_instance != null)
		{
			throw new Error("Instantiation failed");
		}
	}

	public static function getInstance():Singleton
	{
		if(_instance == null)
		{
			_instance = new Singleton();
		}
		return _instance;
	}

}

 

Get Spark Image width or height

So one of the most annoying things about working with display items in the Flex framework is the difference in how things are rendered. This means that after you have created an object in AS3 code you can’t get the properties of that object until after a certain event in the creation process. Generally you need to listen to FlexEvent.CREATION_COMPLETE. After that fires you can usually get width and height etc. the Spark image is a little different. The best way that I’ve found is to listen to Event.COMPLETE and then get sourceWidth or sourceHeight instead of width or height. This one drove me nuts for longer than it should have so hopefully if you have this problem you can find this post before it drives you nuts.

TypeError: Error #1007: Instantiation attempted on a non-constructor

So I know that many people are jumping ship when it comes to Flash and Flex when it comes to web applications but if you are one of the ones like me that is still doing this type of development you might run in to this sometime. The app that I am working on is a Flex app that uses the 4.6 sdk. The is a central module that loads other modules. The problem I ran into was on one of the modules that was using an AdvancedDataGrid. I kept getting the Error #1007: Instantiation attempted on a non-constructor. The solution ending up being something very simple. The main application just needs to have an instance of the AdvancedDataGrid. I simply added AdvancedDataGrid; that along with the import and you should be good to go.

ActionScript Mobile Projects

I’ve been working on more mobile projects lately both at work and in my spare time. I’ve been mainly using the Starling framework and really like it so far. The other day I heard about a new technology being developed by Autodesk that takes flash games written in ActionScript and ports them to Android and iOS. It’s in the early stages, but it looks really promising. To learn more about it go here: http://gameware.autodesk.com/scaleform/advantage

Greatest chocolate chip cookies of all time

So it’s probably weird to read a recipe on my blog, but I actually like to cook. I especially like cooking that involves cookies. Also what programmer doesn’t like to eat cookies. So here is my recipe that has been perfected over time for the greatest chocolate chip cookies.

Chocolate Chip Cookies

  • 1 stick of salted butter (8 tbsp.)
  • 1/2 cup granulated sugar
  • 1/2 cup brown sugar
  • 1 egg
  • 1 tsp. vanilla (real vanilla is best)
  • 1/2 tsp. baking soda
  • 1/2 tsp. salt
  • 1 3/4 cup flour
  • 1 cup chocolate chips
  • 1/2 cup of walnuts (optional)

Preheat oven to 350.

Mix butter and sugar at medium speed until pasty. Add egg and vanilla, mix together at medium speed until creamy. Stir in baking soda, salt, and flour. Add chocolate chips. Drop rounded teaspoonfuls about 2” apart on ungreased baking sheet. Bake 11 minutes.  Let cool slightly before removing from baking sheet. Yields approximately 24 cookies. Enjoy!

I live in high altitude so this recipe has been adjusted accordingly. If you try it and they don’t seem right, try reducing the flour a little or adding a little more butter (coconut oil works great too).

TypeError: Error #1009: Cannot access a property or method of a null object reference – UIMovieClip

So I ran into this error again today. I first ran into it a few years ago. It happens to me when I have an asset that is created in Flash Pro and used in a Flex application. It happens when that component has focus and the whole application then loses focus and then receives focus again. The UIMovieClip class has some issues and so if you are getting this error try one of the following solutions. For either of these solutions you’ll need to put an asset on the root level of your application that extends UIMovieClip, DisplayObject or there are a few others. It has to be an object that the FocusManager instance of the main app can set the focus to. I like to use a logo or some other visual element that I can create in Flash and that will be on every view of your application (root level). What you want to do is before removing any UIMovieclip object from the display list, have the application set the focus to the logo or whatever you’re using. The first way of doing this is by adding the deactivate listener to your flex application. Then have the handler function for that set the focus. This works most of the time, but I’ve found some instances where it doesn’t. If you have text fields that have keyboard listener it doesn’t work. The way I prefer is to dispatch an event from each of my UIMovieClip objects before they are removed (I like to put it in a destroy function). That event bubbles all the way up to the app and then the app calls the function to change the focus. Here is an example:

this.focusManager.setFocus(logo);

That’s it. I hope this might help some of you from pulling your hair out. You want to keep all of the hair that you have, believe me I know.