Question about Injecting data from disk

corey's Avatar

corey

04 Jan, 2012 05:46 AM

Hi,

I have the following model that I want to pre-populate with data from disk before anything Injects the model and creates using the constructor.

public class ProfilesModel extends Actor
    {
        private var _profiles:ArrayCollection;
        private var _namehash:Dictionary;
        public function ProfilesModel()
        {
            super();
            _profiles = new ArrayCollection;
            _namehash = new Dictionary();
        }
}

I am using a sharedObject to store some profile settings on disk that I need to retrieve upon app startup.

How do I tell RL to get from a profilesModel object from disk instead of creating a new model?

Is it good practice to require the use of a service model that handles reading data from a disk in my ProfilesModel? Also do I use DI inside my model?

Basically I need a condition somewhere that says the following:

private var settings:SharedObject = SharedObject.getLocal("settings");
 if ( settings.data[profiles] ){
     profiles = settings.data.profiles

I am just not sure about putting the above code inside my constructor. Any tips? All I want to do is read some saved preferences from disk upon startup and show in a list.

  1. Support Staff 1 Posted by creynders on 04 Jan, 2012 11:39 AM

    creynders's Avatar

    You have to make sure you register a class alias for the ProfilesModel, so it will be serialized and deserialized correctly, when storing and retrieving it in the SharedObject
    Use

    registerClassAlias( "ProfilesModel", ProfilesModel );
    

    or the RemoteClass meta data tag

    [RemoteClass(alias="ProfilesModel")]
    

    Then to use the "retrieved" ProfilesModel instance (it's not really retrieved, but the data is deserialized and automatically a ProfilesModel instance is created) in the DI you have to register it with the injector

    private var settings:SharedObject = SharedObject.getLocal("settings");
    if ( settings.data.profiles != null ] ){
        profiles = settings.data.profiles
        injector.mapValue( ProfilesModel, profiles)
    }else{
        injector.mapSingleton( ProfilesModel );
    }
    

    But TBH I wouldn't do it like this. I'd just save the data to the SharedObject and create a service class that reads it and populates the ProfilesModel instance with the data.

  2. 2 Posted by corey on 04 Jan, 2012 07:55 PM

    corey's Avatar
    [RemoteClass(alias="ProfilesModel")]
    

    This seems to be the magic I was missing. Now where am I supposed to put this code?
    In the ProfilesModel or my SettingsModel Service that retrieves the Object from disk?

  3. 3 Posted by Paul Robertson on 04 Jan, 2012 08:27 PM

    Paul Robertson's Avatar

    The metadata is applied to the class that needs to be able to be stored
    (ProfilesModel in this case):

    [RemoteClass(alias="ProfilesModel")]
    public class ProfilesModel
    {
      ...
    }

    Paul

  4. 4 Posted by corey on 04 Jan, 2012 08:36 PM

    corey's Avatar

    Ok after reading this I was able to figure out that I needed to put the following in my ProfilesModel class before the class statement.

    [RemoteClass(alias="com.lmc.remoteadmin.model.ProfilesModel")]
    

    serialized Objects

  5. 5 Posted by corey on 04 Jan, 2012 10:09 PM

    corey's Avatar

    I got everything sorta working. However there seems to be a difference when I manually instantiate a class vs letting RL instantiate the class through one of the framework methods. When I manually instantiate a class I can no longer dispatch from that class as the eventdispatcher (part of the Actor Class) could not be created and is null.

    TypeError: Error #1034: Type Coercion failed: cannot convert Object@f6d40d1 to flash.events.IEventDispatcher.
    

    However, when I use getInstance the eventdispatcher is created properly.
    For example:

    var smo:SettingsService = injector.getInstance(SettingsService);

    vs

    Manual: No EventDispatcher Created.
    var smo:SettingsService = new SettingnsService();
    or
    var profiles:ProfilesModel = smo.getdata("profiles") as ProfilesModel;

  6. Support Staff 6 Posted by creynders on 05 Jan, 2012 07:51 AM

    creynders's Avatar

    Yes that's correct. Alle dependencies marked for injection are obviously not fulfilled if you create the instance manually (since the DI system plays no part in that). Even though it sometimes seems like it RL doesn't perform any real magic ;)
    You can however let the DI inject the dependencies into the instance with

    injector.injectInto( smo );
    
  7. Ondina D.F. closed this discussion on 10 Feb, 2012 07:43 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac