tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/9384-best-practice-i-need-use-movieclips-stored-in-model-in-my-viewRobotlegs: Discussion 2015-05-11T11:33:27Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/360300082015-02-12T13:20:05Z2015-02-12T13:20:08Zbest practice - I need use movieclips stored in model in my view<div><p>Hello</p>
<p>I have SWFLibsModel which is model that contains various movie
clips.<br>
I also need in my main view use those move clips.</p>
<p>What is best practice ? I tryed to put on my view mediator this
metod, but I dont like this solution. Thank you for advice.</p>
<pre>
<code>private function onSWFLoaded(e:DataEvent):void
{
view.render(swfLibsModel);
}</code>
</pre></div>Egidtag:robotlegs.tenderapp.com,2009-10-18:Comment/360300082015-02-12T14:59:11Z2015-02-12T14:59:11Zbest practice - I need use movieclips stored in model in my view<div><p>Hi Egid,</p>
<p>You are right, passing the model to the view is not nice.</p>
<p>Your Model could dispatch a custom event on the shared
eventDispatcher, with the movie clips as a payload. The Mediator
listens to the event, and passes the payload to the view.</p>
<p>So, if DataEvent is the event dispatched by your SWFLibsModel,
you just need to pass the payload to the view:</p>
<p>view.render(e.payload);</p>
<p>where payload (or a name of your choice) is your movieclip or a
collection (array) of movieclips</p>
<p>Depending on the nature of your movie clips, there could be
other solutions as well.<br>
Are they modules, or just some assets (images, sounds) ?<br>
How are you loading the movie clips and where?</p>
<p>Take a look at Matan's AssetLoader: <a href=
"http://doesflash.com/2010/08/assetloader-robotlegs-tutorial/">http://doesflash.com/2010/08/assetloader-robotlegs-tutorial/</a>
, it might be what you need.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/360300082015-02-17T10:08:40Z2015-02-17T10:08:40Zbest practice - I need use movieclips stored in model in my view<div><p>Hi</p>
<p>Thank you for answer. I apologise for my inactivity. I was off
for few days.</p>
<p>I use my custom AssetLoader, which load *.swf file, which
contains various MovieClips I am using.</p>
<p>My model looks like this:</p>
<p>package assets<br>
{ import controllers.DataEvent; import flash.display.MovieClip;
import org.robotlegs.mvcs.Actor;</p>
<pre>
<code>/**
* @author Egid
*/
public class SWFLibsModel extends Actor
{
private var _swfLibs : Object = new Object();
public function SWFLibsModel()
{
}
public function saveLib(name:String, lib:MovieClip):void
{
_swfLibs[name] = lib;
eventDispatcher.dispatchEvent(new DataEvent(DataEvent.SWF_LOADED));
}
public function getMovieClip(libName:String, clipName:String):MovieClip
{
return _swfLibs[libName].getMovieClip(clipName);
}
}</code>
</pre>
<p>}</p>
<p>I am passing this model to view and call his method getMovieClip
to get any movieClip in library</p>
<p>var mc : MovieClip = swfLibsModel.getMovieClip("LibTiles",
"Bush");</p>
<p>Its for me really handy to have my assets in swf libraries.</p>
<p>I was looking and Matan's AssetLoader and not sure if I should
this assetLoader over my current assetLoading.</p>
<p>Egid</p></div>Egidtag:robotlegs.tenderapp.com,2009-10-18:Comment/360300082015-02-18T12:07:51Z2015-02-18T12:07:51Zbest practice - I need use movieclips stored in model in my view<div><p>No problem:)<br>
You don't have to use Matan's or anyone else's libraries. It was
just a suggestion.</p>
<p>As I said in my previous post, if you don't like to access your
model in your mediator, you can dispatch a custom event from your
model with the movieclip as a payload.</p>
<p>For example:</p>
<pre>
<code>var movieclip: MovieClip = getMovieClip(libName, clipName)
eventDispatcher.dispatchEvent(new DataEvent(DataEvent.SWF_LOADED, movieclip));</code>
</pre>
<p>I don't know when are you loading the movieclips and I also
don't know how many mediators need to pass assets to their views.
Depending on your use case, there might be many solutions.</p>
<p>You could dispatch the event the moment the movieclips are
loaded, or you could let the mediator request the assets within its
onRegister() method by dispatching an event to trigger a command
.</p>
<pre>
<code>override public function onRegister():void
{
//add a listener first
addContextListener(DataEvent.SWF_LOADED, onSwfLoaded, DataEvent );
//then dispatch the event
dispatch(new DataEvent (DataEvent.SWF_REQUESTED, "LibTiles" ));
}
private function onSWFLoaded(e:DataEvent):void
{
view.render(e.payload);
}</code>
</pre>
<p>DataEvent.SWF_REQUESTED could be mapped to a command:<br>
commandMap.mapEvent(DataEvent.SWF_REQUESTED, LoadSWFCommand,
DataEvent);</p>
<p>When triggered, the Command can access SWFLibsModel, which is
injected into the Command. The model can dispatch the event with
the requested assets as a payload to the mediator.</p>
<p>Let me know if you need more help with this.</p>
<p>Ondina</p></div>Ondina D.F.