Announcement Announcement Module
Collapse
No announcement yet.
Event Management and PresentationModels Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Event Management and PresentationModels

    Hi,
    firstly thanks for the great project - much appreciated and making my as3 life easier. I've got a question related to your blog posting

    http://www.herrodius.com/blog/158

    its quite generic and not particularly Spring specific but I come to Spring for best practice aswell as best code...

    So I have a little example which should make for a decent discussion. Let's say we have a set of PlayLists in an MusicManager.

    Each PlayList contains multiple Tracks.

    I want to display the total number of tracks by various dimensions like total rock, ballads etc.

    So what I've done is have a single MusicManager model instance, and I created a TypedCollection in this of PlayLists (see its a bit Springy?), and a PlayList has a TypedCollection of tracks.

    Code:
    class MusicManager {
         private var _playLists:TypedCollection = new TypedCollection(PlayList);
    
         public function addPlaylist(playList:PlayList):void {
            playList.tracks.addEventListener(CollectionEvent.COLLECTION_CHANGE, refresh);        
             _playLists.addItem(playList);
         }
    
         public function get playLists():TypedCollections {
             return _playLists;    
         }
    
         public function refresh():void {
             //do some interesting aggregation by rock, balad
         }
    }
    
    class PlayList {
       private var _tracks:TypedCollection = new TypedCollection(Track);
    }
    So you see how the MusicManager adds itself as an EventListener to the playList's tracks collection. So this works up to a point in that when you add and remove tracks from playlists the MusicManager can update itself, however if you simply modify an existing track in a PlayList, say rename it, then the PlayList tracks collection doesn't see that and so no events get fired and no screen updates.

    We've also had another problem - namely that other developers aren't using the special addPlayList method, they were accessing the property directly and then not getting the event support.

    Also currently if you update a tracks on a playlist as a caller you have a responsibility to call refresh on the PlayList's tracks.

    This is all a bit sucky I know, so I'm looking for a remedy.

    So one way would be to force all modifications of Tracks to go via the parent PlayList. playlist.updateTrack(track); and this way the playlist can see one its managed tracks has been updated an update it.

    An alternative I've been toying with is making each Track an EventDispatcher in its own right. This would then fire events whenever it changed. When you added a Track to a PlayList then the PlayList would register as a listener to the Track. Decoupled but complex.

    But these don't get around the problem that the lists are still mutable and developers could still access these directly and miss any wiring code in the explcity addX methods.

    So this is why I referred to the blog entry. I went on to look at MVCS and I've used adaptors in the past with Swing. I'm thinking keep the model only hand out immutable copies of its current list and also have the model fire events. That way an adaptor can take a model as a constructor, get a copy of the current state and add itself as a listener to the underlying model. This way there is no ambiguity in the model, you cannot access and modify the underlying collection directly - you have to use the addX methods.

    The adaptor can be a little more relaxed. Does anyone else has experience and any best practice advice?

    thanks,

    Neil

  • #2
    Hi, as you can see i posted my very first spring-actionscrip post yesterday. And since this part of the forum does not seem to be very popular and one might wait a long time for replies, i tell you the first thing that came to my mind when i read your question:

    Extend the TypedCollection class. Override "addItem" and everything that's relevant for you.
    Last edited by Lori; Mar 4th, 2009, 06:03 AM.

    Comment


    • #3
      Thanks Lori

      Hi,
      of course Lori. I've overlooked this very simple and extremely effective solution to my problem! Thanks.
      Neil

      Comment


      • #4
        You're welcome!

        Comment

        Working...
        X