Call a Method in a Collection - actionscript-3

I've got a collection of custom objects with a method, Update(), in each of them. I need a way to iterate over the collection and call that method. This is easy to do in C# with a List, but I'm not sure how to do it in ActionScript. Any help that you could provide would be greatly appreciated. Thanks!

have a look at Array.foreach()
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#forEach%28%29

So, after some experimentation, I found that you can type the iteration variable in the look to the object that the list is composed of, and it opens up all the methods and properties in that object:
for each(var ship:Ship in ships)
{
ship.Update();
}
Not too sure if this is GOOD to do or not, but it works...

Related

getIn() not traversing an Immutable structure

I have an OrderedMap called "firebase" from the immutable-js library which has three leaf nodes:
but then I'd expect to be able to inspect the ordered map at firebase.auth with:
newValue.getIn(['firebase', 'auth'])
But that doesn't work:
Can someone help me understand what I'm doing wrong.
Your auth is an Immutable OrderedMap, but it seems that your firebase is a simple JavaScript object, not an Immutable.js one. Hence why getIn would not work. Thus newValue.get('firebase').auth would suffice.
If you are not using the redux-immutable package then you are probably going to have these (and other) issues due to combineReducers. However, combineReducers created using redux-immutable uses Immutable.js API to iterate the state so check it out.

Calling functions between components in Coldfusion

Say I have a project, I have several CFC's in that project. For the sake of the example, I have a debugging function general.cfc and I want to use that function in mandrill.cfc without having to copy and paste the code into the latter cfc. I've googled this until I can google no more and I know I need to post it here.
Anyone care to take me to school?
This is how I would do it
<cfset var objGeneral = createObject("component","general")>
Then if you have a function in general.cfc called getName() you can call it by saying objGeneral.getName()
You should look at the notion of dependency injection, and you should specifically look at how ColdSpring implements it in the context of ColdFusion (or DI/1).
Basically you have an init() argument in Mandrill which would take a General object, and then you set the General object into the variables scope of the Mandrill object, using its methods via variables.general.
That said, this works best on singleton objects. If you need to do this sort of thing on a transient object, I'd just instantiate the General object as needed within your Mandrill code (ie: now Matt Busche is suggesting).
My solution was to have mandrill.cfc extend the general.cfc component:
<cfcomponent extends="general" name="mandrill" ...>
See also CreateObject

SharedObject and Dictionary problems in Flash Builder

I'm usin Flash Builder to create some actionscript code that uses SharedObjects.
First question: how can I delete my local SharedObject in Flash Builder? I am debugging my program and the SharedObject sems to persist between runs. I want to start fresh and clean with no SharedObject storing my data. How do I get rid of it?
Also, in my SharedObject, I used mySharedObject.data["mykey"] to store a Dictionary. This Dictionary will have String keys and values of MyCustomClass. The problem is that when I later try to loop over the values of this Dictionary, I get error #1034 cannot convert object to type MyCustomClass. It seems like I can put an item of type MyCustomClass into this dictionary, but I can't get the item back out as anything other than an object.
Any idea what is going wrong?
Those are essentially two questions, so should have been asked as two questions. Anyway, I'd answer them here but still prefer that you break them up in two parts (possibly leave a link to the other one here for reference sake):
Local shared object, are useful exactly for persistence across runs. And then there's SharedObject.clear() to clear the state as required.
For you second issue, Shared Object's serialize your object into AMF, so that it can be written to disk or sent over network using RTMP. Now, your custom class can't really be serialized in AMF. What actually happens is that the public properties (and dynamic ones, if the class is declared dynamic) are serialized into the structure. So, the public data is stored... but it's essentially a general Object.
To work around that, you can have a public static readFrom(object:Object):MyCustomClass type function, which would read the properties from the passed object to construct a new MyCustomClass representing that information.
There are ways to register your class with the player to be stored in SharedObject (see here)... but you need to make sure that the code that de-serializes that data is aware of the class as well.
To make a class available for conversion, in your global initialization use registerClassAlias() call with MyCustomClass and its fully qualified name as parameters. The manual. Say your custom class is foo.bar.TheClass, you write:
registerClassAlias('foo.bar.TheClass',foo.bar.TheClass);
In order to drop old SO use delete call against so.data["mykey"] and do so.flush(). Edit: SharedObject.clear() is way better.
1/ Being persistent is one of the particularity of a SharedObject. To cleanup all its content, you need to call the clear method.
var shareObject:SharedObject = SharedObject.getLocal('justatest');
shareObject.data.test = 'test';
trace(shareObject.data.test)
shareObject.clear();
trace(shareObject.data.test)
output
test
undefined
2/ To store complex data types in SO, you need to use flash.net.registerClassAlias (example here)

Finding DisplayObject to use in PopUpManager

I am creating an extension for a Flex Web Client. The way my extension is introduced is through a menu item in the UI. When the user selects the action from the menu, it will run my specific ActionScript method.
I would like to have my ActionScript method create a pop up using PopUpManager. The issue I am running into is that the first argument for PopUpManager.createPopUp is a DisplayObject. Since I am going straight into a AS method, I cannot use an inherit DisplayObject (aka 'this').
I am looking for a way from the AS method to find or access a reference to a parent/root DisplayObject. Is this possible? I am pretty new to Flex/ActionScript, so I may also have a fundamental misunderstanding of what is going on here.
Thanks in Advance!
Going with a Self-Answer for anyone else that is searching for this topic, from my earlier comment
import mx.core.FlexGlobals
var popup:IFlexDisplayObject = popUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, MyPopUp, false); PopUpManager.centerPopUp(popup);

Is there a way to access entry point class object from other classes of the app?

Just wonder if I can reference entry point object from any place in the app? Is it maybe assigned to some global object, like stage is for example?
Currently I use static methods and variables, but this breaks encapsulation.
If someObject is in display list, then you have a someObject.root reference which is what you are looking for. If you remove someObject from display list, you loose that reference.
My answer is no, there is no direct way to access entry object, and there shouldn't be: that's what incapsulation is about. Accessing something globally is pretty much AS1 way. When you access main instance by implicitly referencing MainClass, you make parts of your application tightly coupled, which is generally bad.
However, if you do need to have it, you may choose from several options.
Use static var: MainClass.instance
Use singletone-like access through MainClass.getInstance()
Create a package-level variable or a package level getter method
I would choose the latter.
package com.smth.application
{
public var mainObject:MainClass;
}
// in main app
package com.smth.application
{
public function MainClass()
{
mainObject = this;
}
}
It may look somewhat similar to static acces, but I think this way your code will retain some flexibility.
stage is a reference to the top level of your application, which you can access though any display object that is on the display tree as myDisplayObject.stage. When using a custom document class, it will be the first child (index 0) of stage, unless you manually force something else into the 0 index as Bakapii says.