Chapter 8. Advanced Model Topics

In this chapter, we will have a look at a couple of common Model related issues: Synchronous versus Asynchronous behavior, and reuse of the Model tier.

Handling Synchronous Behavior

In the StoryArchitect application, our Proxy subclasses interacted with the filesystem in a synchronous fashion. That is to say, the result of reading a file is available to the next line of code, and you may begin working with the retrieved data immediately. This is nice because the calling code is usually in the middle of doing something when it makes the call and would probably like to get on with it.

Here is an example from StoryArchitect of a Proxy method and the calling Command using the fetched data immediately as it carries out a use case. The ApplySelectionCommand has to select a Story (a view-related endeavor), but needs to be sure that if the Story is a stub (as it would be from a list), it is fully loaded when placed on the SelectionContext. Notice the ease with which the result is consumed in the Command.

The Story Proxy

Class

StoryProxy.as

Method

loadStory()

Code

 /** * Load a Story. * * If Story is already cached from having been * previously loaded or added, the cached VO will be * returned, otherwise it will be loaded and cached first. * * Optionally allows forced re-caching from disc * (as when user changes are discarded). */ public function loadStory( storyStub:StoryVO, recache:Boolean = false ):StoryVO { // Optionally force loading from disk and recaching if ...

Get ActionScript Developer's Guide to PureMVC now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.