Cookies for Robotlegs via SharedObject

Posted 2 October 2010 by

Working with the SharedObject can sometimes be a real pain, I find a lot of AS3 developers don’t even know about the SharedObject and what’s possible with it. — Correct me if I’m wrong! Using cookies for a Flash website/application can really add that extra little bit of value, stuff that user might “expect”. Like a “remember me” check box at a login form or saving whether the user has muted/changed the volume, etc. You get the point.

I’ve put together a small class that will help you manage/create/delete cookies via the SharedObject with ease. In the example I’ll be using it with Robotlegs, but you don’t have to. The class does not need any injections, thus you can construct it where and when you want.

Stuff that annoyed me before writing this class:

  • Setting up the SharedObject.
  • Checking whether the user has granted permission to local storage.
  • Checking if cookies exist.
  • Setting defaults.
  • Managing what cookie names you have used.

There might be more, but I’ll stop there.

What will the CookieModel do for you?

  • Setup the SharedObject.
  • Only ask the user once for permissions. If the user grants permission, your values will be stored on the local machine. If the user denies access, the CookieModel will automatically create an internal Object that saves your values. This means that it will continue to work as expected, you don’t have to fall back onto other variables keeping your data. Only thing to note is that this data will not be retained once the application restarts. But it cuts out a lot of extra work!
  • Setup default values, so you don’t have to do manual checking for saved values. This is useful when you need retrieve data from the SharedObject, but you also need to value to be present for use.
  • Keeps all cookie names used. So you can iterate over them if you want to.

Using the CookieModel with Robotlegs:

In you Context simply create a value mapping of you new CookieModel instance to the ICookieModel interface.

injector.mapValue(ICookieModel, new CookieModel("some-name-related-to-app"));

Now you can [Inject] the ICookieModel where ever you need it; Commands, Mediators, Models, Services, etc.

[Inject]
public var cookies : ICookieModel;

Manage your cookie ids:

You need easy reference to your cookies, so create a class called CookieId and create public static String constants for all your cookie ids. FDT can do this via a quick fix, super awesome.

package mu.model.ids
{
	public class CookieId
	{
		public static const USER_NAME : String = "USER_NAME";
		public static const VOLUME : String = "VOLUME";
	}
}

Using it:

Here is a simple command injecting the ICookieModel and then saving two values.

package mu.controller
{
	import mu.model.ICookieModel;
	import mu.model.ids.CookieId;
	import org.robotlegs.mvcs.Command;

	public class CreateDefaultCookiesCommand extends Command
	{
		[Inject]
		public var cookies : ICookieModel;
		override public function execute() : void
		{
			// Will overwrite previous value if it existed or create the cookie if it didn't.
			cookies.setCookie(CookieId.USER_NAME, "Matan");
			// Will only set the VOLUME to 1 if the cookie does not exist.
			cookies.setCookieDefault(CookieId.VOLUME, 1);
		}
	}
}

The Cookie Tester with source:

Play with the values, reload the page, deny access, grant access, go crazy!

Thanks goes to Robotlegs, MinimalComps, FDT.

Download Source:

Great! As always, hope you enjoy and feel free to comment!

Post Details