If you’re being a good Flex / AS3 dev, you are probably wrapping your class member properties with public getter and setter methods.
If you’re not, you really, really should…
Getters and Setters, otherwise known as accessor methods, or mutator methods (among other things), are very helpful. They allow you to make your public properties have a far greater set of controls and features. I won’t go into all the details here, I’m assuming that you, like me, only fail to set them up because typing…
public var classMember : String;
Is slightly quicker than typing…
private var _classMember : String;
public function set classMember( value : String ) : void {
_classMember = value;
}
public function get classMember( ) : String {
return _classMember;
}
Anyway, if you are an FDT user, (or your chosen IDE has a templating feature) you can create a template to do this for you.
Read/Write member … (template name member)
private var _${name} : ${type};
public function set ${name}( value : ${type} ) : void {
_${name} = value;
}
public function get ${name}( ) : ${type} {
return _${name};
}
And then it will only take as long as setting up a public var, but with all the goodness that comes with using getter/setters…
You could setup templates for a read only member, and a write only member too…
Read only member … (template name rmember)
private var _${name} : ${type};
public function get ${name}( ) : ${type} {
return _${name};
}
Write only member … (template name wmember)
private var _${name} : ${type};
public function set ${name}( value : ${type} ) : void {
_${name} = value;
}
July 30th, 2008
I'm using Trac on a fairly large project, one of the main obstacles for usage is having to use the Web interface to write tickets, wiki-pages so on...
The majority of the 30+ strong team haven't used Trac before, and with all tools, uptake is the most important factor. A large proportion of the team are all Mac based so I'm initially creating tools on that platform.
TextMate a very popular text editor, a favourite of Ruby on Rails developers world-wide, has a bundle (or in regulo-speak, a plugin) for Trac wiki text.
I'm using this a starting point and adding some helper utilities to make the process of using Trac less of a bother.
The first one is the simplest, generate an HTML preview of wiki text from TextMate
If you have TextMate installed and have added the Trac Wiki bundle you can open the Bundle editor and add a New Command (call it Preview, if you like).
Insert the following ruby code :
#!/usr/bin/env ruby
##################################################
# Trac Wiki Previewer for TextMate
# (Uses Trac XMLRPC method 'wiki.wikiToHtml')
##################################################
# jason@mentalaxis.com - Released under MIT Licence
##################################################
$LOAD_PATH << "#{ENV["TM_SUPPORT_PATH"]}/lib"
require 'xmlrpc/client'
##################################################
# In TextMate preferences setup the following vars...
#
# TM_TRACURL : Typically 'http://your_trac_env_url/xmlrpc'
# TM_TRACUSER : trac username
# TM_TRACPASSWORD : trac password
#
tracServer = XMLRPC::Client.new2(ENV["TM_TRACURL"])
tracServer.user = ENV["TM_TRACUSER"]
tracServer.password = ENV["TM_TRACPASSWORD"]
puts tracServer.call("wiki.wikiToHtml", STDIN.read)
As you'll notice in the code, you need to setup 3 environment vars to make the script work. You'll also need web access to a Trac environment
- TM_TRACURL : Typically 'http://your_trac_env_url/xmlrpc'
- TM_TRACUSER : trac username
- TM_TRACPASSWORD : trac password
Set the command options for Selected Text or Document and output to HTML
You can now view your Trac wiki text as plain HTML from within TextMate
July 23rd, 2007
If you like the theme I’ve made for Tumblr, you can grab a copy for your own Tumblr site.
it’s a copy and paste affair
Select custom theme on your Tumblr settings page. Copy and paste to replace the code inside custom theme textarea on the Tumblr settings page. Then Save settings…
Hover here for a Snap preview…
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="color:Background" content="#eee"/>
<meta name="color:Content Background" content="#fff"/>
<meta name="color:Title" content="#444"/>
<meta name="color:Description" content="#666"/>
<meta name="color:Post Title" content="#c00"/>
<meta name="color:Text" content="#444"/>
<meta name="color:Inline Link" content="#c00"/>
<meta name="color:Quote" content="#666"/>
<meta name="color:Quote Source" content="#444"/>
<meta name="color:Link Post" content="#c00"/>
<meta name="color:Conversation Background" content="#f8f8f8"/>
<meta name="color:Conversation Border" content="#ddd"/>
<meta name="color:Conversation Text" content="#444"/>
<meta name="color:Conversation Label" content="#111"/>
<meta name="color:Photo Border" content="#eee"/>
<meta name="color:Date" content="#fff"/>
<meta name="color:Date Background" content="#ccc"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="alternate" type="application/rss+xml" title="RSS"
href="/rss"/>
</head>
<body>
<div id="content">
<h1><a href="/">{Title}</a></h1>
{block:Description}
<div id="description">
<div>{Description}</div>
</div>
{/block:Description}
{block:Posts}
<div class="post">
<div class="labels">
{block:NewDayDate}
<div class="date"><a href="{Permalink}">{Month} {DayOfMonth}</a></div>
{/block:NewDayDate}
{block:SameDayDate}
<div class="date"><a href="{Permalink}">+</a></div>
{/block:SameDayDate}
</div>
{block:Regular}
<div class="regular">
{block:Title}<h2><a href="{Permalink}">{Title}</a></h2>{/block:Title}
{Body}
</div>
{/block:Regular}
{block:Photo}
<div class="photo">
<img src="{PhotoURL-400}" alt="{PhotoAlt}"/><br/>
{block:Caption}
<div class="caption">{Caption}</div>
{/block:Caption}
</div>
{/block:Photo}
{block:Quote}
<div class="quote">
<span class="quote">
<big class="quote">“</big> {Quote}
</span>
{block:Source}<span class="source">{Source}</span>{/block:Source}
</div>
{/block:Quote}
{block:Link}
<div class="link">
<a href="{URL}" class="link" {Target}>{Name}</a>
{block:Description}
<span class="description">{Description}</span>
{/block:Description}
</div>
{/block:Link}
{block:Conversation}
<div class="conversation">
{block:Title}<h2><a href="{Permalink}">{Title}</a></h2>{/block:Title}
<ul>
{block:Lines}
<li>
{block:Label}<span class="label">{Label}</span>{/block:Label}
{Line}
</li>
{/block:Lines}
</ul>
</div>
{/block:Conversation}
{block:Video}
<div class="video">
{Video-400}
{block:Caption}
<div class="caption">{Caption}</div>
{/block:Caption}
</div>
{/block:Video}
</div>
{/block:Posts}
<div id="footer">
{block:PreviousPage}
<a href="{PreviousPage}">« Previous</a>
{/block:PreviousPage}
{block:NextPage}
<a href="{NextPage}">Next »</a>
{/block:NextPage}
</div>
</div>
</body>
</html>
June 20th, 2007
This is a first draft solution to creating XML document from an Object structure (when E4X isn't available < flash9 / AS3) a friend of mine was asking if this was possible within Flash as a standard class, since it's not I thought about it and I decided it'd be quite easy to build, so I wrote this class based on Darron Schall's TraceObject function.
The class provides static methods to recurse an Object tree and create a corresponding XML document.
This is a rough first draft, proof of concept, which I'll try and improve over the next few days, there are a few things that I think it'll need, creating node attributes or CDATA sections from an Object, the thing is, these will require an Object to be structured in a particular fashion ... Something it'd like to avoid, but in some cases this functionality would be extremely useful.
Any suggestions for object formatting to provide CDATA / attribute notation would be well recieved.
Anyway here you go, an XML serialiser for Flash, it should work with all versions of Flash v6 up. (I've only tested it with Flash Player 8 & 9.)
/**
* A static helper class to create an XML document from an Object
* structure.
*
* @usage var myXMLDoc : XML = XMLSerialiserAlpha.seralise(myObject);
* Where myObject contains an object structure
* eg. http://en.wikipedia.org/wiki/JSON#JSON_example
*
* @author jason at mentalaxis dot com
*/
class XMLSerialiserAlpha{
private function XMLSerialiserAlpha(){}
private static var mInit : Boolean = false;
private static var mTabs : String = "";
private static var docString : String;
private static var xmlDom : XML;
private static function _out (strValue : String) : Void {
docString += strValue + "n";
}
private static function docStringToXML() : XML {
xmlDom = new XML(docString);
return xmlDom;
}
/**
* Recursively trace an object structure and send
* string fragments to the _out method, the user
* call returns an XML object.
*
* @param obj Object
* @param inOpenChar String
* @param initialBlockDisplayed Boolean
*
* @return the xml object to the external call
*/
public static function seralise(obj : Object, initialBlockDisplayed : Boolean, nodeName : String ) : XML {
mInit = (initialBlockDisplayed) ? false : true;
var topLeaf : Boolean;
if (mInit) {
docString = "";
mInit = false;
topLeaf = true;
} else {
topLeaf = false;
mTabs += "t";
}
for (var i : String in obj) {
if (typeof (obj [i]) == "string") {
_out (mTabs + "<"+i+">" + obj [i] + "</"+i+">");
} else if (typeof (obj [i]) == "object") {
if (obj [i] instanceof Array) {
seralise(obj [i], true, i);
} else {
if(nodeName != undefined) {
_out (mTabs + "<"+nodeName+">");
} else {
_out (mTabs + "<"+i+">");
}
seralise(obj [i] , true);
if(nodeName != undefined) {
_out (mTabs + "</"+nodeName+">");
} else {
_out (mTabs + "</"+i+">");
}
}
} else {
_out (mTabs + "<" + i + ">" + obj [i] + "</" + i + ">");
mInit = true;
}
}
mTabs = mTabs.substr (0, mTabs.length - 1);
if(topLeaf)
{
return docStringToXML();
} else {
return null;
}
}
}
Of course if you find any bugs or have suggestions for improvement, I'm interested to hear about them.
July 6th, 2006
This Timeout class provides a timer which will dispatch an “onComplete” event message, when the timer is complete.
import mx.utils.Delegate;
import mx.events.EventDispatcher;
class Timeout {
private var timerInterval : Number;
private var duration : Number;
private var destroyed : Boolean = false;
private var running : Boolean = false;
public function addEventListener(type : String, handler : Function) : Void {}
public function removeEventListener(type : String, handler : Function) : Void {}
private function dispatchEvent(e : Object) : Void {}
/**
* Timeout provides a timer which will dispatch an onComplete message.
*
* @usage
* function myTimeoutHandler() {
* trace("Timer complete");
* // do something after timeout?
* }
* var duration : Number = 1; // duration as Seconds
* var myTimeout : Timeout = new Timeout(duration);
* myTimeout.addEventListener("onComplete", myTimeoutHandler);
* myTimeout.go();
*/
public function Timeout(duration : Number) {
EventDispatcher.initialize(this);
if(duration != undefined) {
this.duration = Math.abs(duration)*1000;
}
}
public function go() : Void {
if(!running) {
running = true;
setTimeout();
} else {
restart();
}
}
private function setTimeout () : Void {
timerInterval = setInterval (Delegate.create(this, timeoutComplete), duration);
}
public function restart (newDuration : Number) : Void {
if(newDuration != undefined) {
duration = newDuration;
}
dispose();
go();
}
public function dispose () : Void {
clearInterval(timerInterval);
running = false;
}
public var crush : Function = dispose;
public var kill : Function = dispose;
public var destroy : Function = dispose;
public function toString() : String {
return "[Object Timer : "+ timerInterval + " ]";
}
[Event("onComplete")]
private function timeoutComplete ():Void {
clearInterval(timerInterval);
dispatchEvent({type:"onComplete", target:this});
}
}
Usage
function myTimeoutHandler() {
trace("Timer complete");
// do something after timeout?
}
var duration : Number = 1; // duration as Seconds
var myTimeout : Timeout = new Timeout(duration);
myTimeout.addEventListener("onComplete", myTimeoutHandler);
myTimeout.go();
If you’d like to stop the Timer before it’s completed…
myTimeout.dispose();. Or you can use myTimeout.crush();, myTimeout.kill();, myTimeout.destroy(); depending on your mood.
You can restart the timer myTimeout.restart(); and change the timer duration myTimeout.restart(2);
April 20th, 2006
Previous Posts