ActionScript Development
that makes the component functional, see (Listing 2). This code is responsible for setting the title and body of the blog post item and handling the click of the LinkButton. The setPostBody() method is responsible for limiting the length of the text description to 80 characters, and if the length is indeed longer than 80 characters, an ellipsis is added to identify there is more text. Also, if you notice, the event handler is simply dispatching a custom event of "readFullPost" , which will be defined in the next section of the main MXML file. This event will trigger the display of the full post. At this point the MXML component is completed, save your changes and move onto the main.mxml file to get everything working together. The first step of this section is to add the other components to complete the design of the blog view, see (Listing 3). The majority of the MXML is standard, the design portion in this code is the single post view, that is not visible to start. The Application tag has a creationComplete event that is called once the application is fully loaded. Now that the design is complete, the next step is to build the ActionScript, which is all contained within
Listing 8. The event handler that is called once the XML is loaded
private function postDataLoaded(e:Event):void {
var urlLoader:URLLoader = URLLoader(e.currentTarget); var nextY:uint = 50; var i:uint=0; posts = []; for each ( var post:XML in XML(urlLoader.data)..post) {
. . .
}
}
Listing 9. The for..each loop responsible for creating a new BlogItem instance for each XML 'post' node
for each ( var post:XML in XML(urlLoader.data)..post) { posts.push({ id:post..id, label:post..name, data:post..body }); var blogItem:BlogItem = new BlogItem();
container.addChild(blogItem);
blogItem.itemID = i; blogItem.postTitle = post..name; blogItem.setPostBody(post..body); blogItem.x = 10; blogItem.y = nextY += blogItem.height + 5; i++;
}
Listing 10. Method called when the LinkButton is clicked, to view the single post
private function fullPostHandler(e:MouseEvent):void {
trace("fullPostHandler() " + BlogItem(e.target).itemID); container.visible = false ; container.height = 1;
singlePostTitle.text = posts[BlogItem(e.target).itemID].label; singlePostBody.text = posts[BlogItem(e.target).itemID].data; singlePost.visible = true ;
}
Listing 11. The last method in the application, used to hide the single post view and re-display the list of posts
private function postListViewHandler():void {
singlePost.visible = false ; container.visible = true ; container.percentHeight = 100;
}
03/2009 (5)
66
Made with FlippingBook HTML5