tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/769-question-about-injecting-data-from-diskRobotlegs: Discussion 2012-02-10T07:43:54Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/126475942012-01-04T11:39:01Z2012-01-04T11:42:13ZQuestion about Injecting data from disk<div><p>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<br>
Use</p>
<pre>
<code>registerClassAlias( "ProfilesModel", ProfilesModel );</code>
</pre>
<p>or the RemoteClass meta data tag</p>
<pre>
<code>[RemoteClass(alias="ProfilesModel")]</code>
</pre>
<p>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</p>
<pre>
<code>private var settings:SharedObject = SharedObject.getLocal("settings");
if ( settings.data.profiles != null ] ){
profiles = settings.data.profiles
injector.mapValue( ProfilesModel, profiles)
}else{
injector.mapSingleton( ProfilesModel );
}</code>
</pre>
<p><strong>But TBH I wouldn't do it like this.</strong> 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.</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/126475942012-01-04T19:55:07Z2012-01-04T19:55:07ZQuestion about Injecting data from disk<div><pre>
[RemoteClass(alias="ProfilesModel")]
</pre>
<p>This seems to be the magic I was missing. Now where am I
supposed to put this code?<br>
In the ProfilesModel or my SettingsModel Service that retrieves the
Object from disk?</p></div>coreytag:robotlegs.tenderapp.com,2009-10-18:Comment/126475942012-01-04T20:27:37Z2012-01-04T20:27:37ZQuestion about Injecting data from disk<div><p>The metadata is applied to the class that needs to be able to be
stored<br>
(ProfilesModel in this case):</p>
<p>[RemoteClass(alias="ProfilesModel")] public class
ProfilesModel<br>
{ ... }</p>
<p>Paul</p></div>Paul Robertsontag:robotlegs.tenderapp.com,2009-10-18:Comment/126475942012-01-04T20:36:29Z2012-01-04T20:36:29ZQuestion about Injecting data from disk<div><p>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.</p>
<pre>
[RemoteClass(alias="com.lmc.remoteadmin.model.ProfilesModel")]
</pre>
<p><a href=
"http://cookbooks.adobe.com/post_How_to_keep_the_type_of_your_objects_when_serializ-8323.html">
serialized Objects</a></p></div>coreytag:robotlegs.tenderapp.com,2009-10-18:Comment/126475942012-01-04T22:09:11Z2012-01-04T22:09:11ZQuestion about Injecting data from disk<div><p>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.</p>
<pre>
TypeError: Error #1034: Type Coercion failed: cannot convert Object@f6d40d1 to flash.events.IEventDispatcher.
</pre>
<p>However, when I use getInstance the eventdispatcher is created
properly.<br>
For example:</p>
<p>var smo:SettingsService =
injector.getInstance(SettingsService);</p>
<p>vs</p>
<p>Manual: No EventDispatcher Created.<br>
var smo:SettingsService = new SettingnsService();<br>
or<br>
var profiles:ProfilesModel = smo.getdata("profiles") as
ProfilesModel;</p></div>coreytag:robotlegs.tenderapp.com,2009-10-18:Comment/126475942012-01-05T07:51:59Z2012-01-05T07:51:59ZQuestion about Injecting data from disk<div><p>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 ;)<br>
You can however let the DI inject the dependencies into the
instance with</p>
<pre>
<code>injector.injectInto( smo );</code>
</pre></div>creynders