How to read all data from abstract Shared Object file? (Flash, Actionscript) - apache-flex

what I meen is to get all objects and their properties, is it possible? How to do such thing?

Read the SharedObject. The documentation is a great start.
Loop through the properties. Depending on how you stored them if they are simple, a for...in loop should do it. If you are storing complex types (like an Object inside and Object, etc.), you need to first check if the object is simple or not. If it is not, you need to loop through its properties.
Luckily, since you've tagged this for flex as well, the ObjectUtils class will be handy for things like checking if an Object isSimple() or inspecting an Object's properties via getClassInfo().
e.g.
//assuming nestedObjectFromSO is a nested object read from a SharedObject
trace(ObjectUtil.getClassInfo(nestedObjectFromSO).properties);
Depending on your structure, you might need to write a recursive function that gets the data from nested objects within your SharedObject.
HTH,
George

You can use AIR Shared Object Editor to grab and edit .sol files.

Related

Flex Air: Read "ActionScript object" from file then cast?

It is quite simple to do it, you write the object down to file, then you read it:
http://corlan.org/2008/09/02/storing-data-locally-in-air/
http://cookbooks.adobe.com/post_Storing_ActionScript_Objects_in_the_Encrypted_Loca-10563.html
My questions are
why when we put [RemoteClass(alias="foo.Bar")] into VO, it can be
cast automatically (otherwise the type of the deserialized object is
Generic Object with correct properties data inside it)?
Is there another way to achieve it without RemoteClass tag? (Using metadata tag is preference)
Thanks
The answer is in the page you linked to:
Note that the alias is a key that is stored with the class instance
and links the class definition with the specific object that is stored
in the ByteArray when an instance of that object is serialized. This
key can be any unique string identifying this class, but convention is
to use the fully normalized package and class name.
That's why you get a generic object if you omit the alias - the deserialization method does not know what to do with the data, unless you specify to which class the values should be mapped.
Yes, there is: registerClassAlias() does the same thing. But the metadata tag is much prettier to read ;)

Accessing an object's workflow state in collective.easytemplate

I would like to use collective.easytemplate to generate templated emails (for content rules). However, I am not sure if it can output an objects workflow state. Anybody know if it is possible and how it is done?
Thanks.
You can, it is possible, and one way is to use the portal_workflow tool e.g. from parts/omelette/plone/app/contentrules/tests/test_action_workflow.py:
self.assertEquals('published',
self.portal.portal_workflow.getInfoFor(self.folder.d1, 'review_state'))
More generally, something like:
context.portal_workflow.getInfoFor(context, 'review_state')
in a page template should work. Or use the portal_catalog as Spanky suggests e.g. if "obj" is a catalog "brain" (i.e. part of a result set from a catalog search) then:
obj.review_state
should work.
The portal_catalog also has an index of the workflow's Review State, so if you don't already have the object you're working on (e.g. context ≠ the object) you could use the catalog, look up the object and get the review state from the resulting "brains" object.
Apparently there are ALSO browser view methods available to you as well, and I notice that one of them is workflow_state. See:
http://plone.org/documentation/manual/theme-reference/page/otherinfo

How to store object types and create objects using that information only

I would like to store MetaObjects in a list like this:
myList = QList<QMetaObject>();
myList->append(MyClass::staticMetaObject);
myList->append(MyOtherClass::staticMetaObject);
I want to keep track of these object through out the application but I don't wish to allocate them just yet. By adding some information in my classes I will be able to use the MetaObject function "classInfo(int).value()". I use this when I store them in a QListWidget. When a row in the QListWidget is pressed I would like to be able to create an object of that specific kind that is stored in the list.
(Also have to add that all the classes dervies from the same baseclass)
This sample code describes a bit of what I want to do, except in his example, you add the classes as you go along.
http://lists.qt.nokia.com/pipermail/qt-interest/2012-January/037204.html
I read through the manual and when I try things like:
MyBaseClass *test = qobject_cast<MyBaseClass*>myList->at(i).newInstance();
The project compiles but when I try to print the "test" object its null. What am I doing wrong? And is this the best way of doing this?
Was also looking at MetaType, but where would i be able to store, for example a string for the menus if I'm not allowed to create the object? Would this be a nicer solution if I have a static function that returns a string?
Edit:
I now changed so the constructors are Q_INVOKABLE which solved the problem where "test == null".
But what are the downside of this solution? Should I just use a object factory (the old fashion way with a switch case)?

Objects added to QList lose all of their member data

I'm making a QList of a custom class called ControlIcon. I create the ControlIcons, and load them up with member variables, and then add them to the list. Here's the append code:
this->cueList.append(firstOne);
Here's the declaration of the QList:
QList< ControlIcon *> cueList;
If I break right after the append, I can see that the ControlIcon that was just added is full of members, and appears fine. I look in the list, and the ControlIcon that's been appended (and it does append a ControlIcon) has no members at all. I've made a QList of custom objects before, so I'm very confused. Could someone help?
Your custom class must be assignable, that means it must provide a default constructor, a copy constructor and a assignment operator. If they don't, weird things like this can happen.
cueList contains pointers to ControlIcon objects, but it is not responsible for creating or keeping those ControlIcon objects. How did you get the pointer firstOne? If it points to something on the stack, that object would be invalid when you try to use it later. If it was created with new, it will stay valid until you clean it up using delete.
I changed the data type of the list to be SerialController (the child of ControlIcon), which is what I was trying to add to it, and it works fine. This is really annoying, because there's other types of controllers, but I guess it will have to work for now.

Why does Flex's ArrayCollection's Contain method look at memory reference?

When using .contains() on an ArrayCollection in Flex, it will always look at the memory reference. It does not appear to look at an .equals() method or .toString() method or anything overridable. Instead, I need to loop through the ArrayCollection every time and check each individual item until I find what I'm looking for.
Does anyone know why Flex/ActionScript was made this way? Why not provide a way from people to use the contains() method the way they want?
Couldn't you just extend ArrayCollection and override the contains() method? Alternatively you can paste the source for ArrayCollection into an "mx/collections" package in your project and modify the source; this "monkey-patching technique" will override the behavior throughout your entire project. However I would be extremely cautious about changing ArrayCollection in that manner: since it's used all over the place in the Flex APIs there is a good chance you'll start breaking other components in the framework.
The contains() method searches by reference, correct (I believe even for primitives), so if you're trying to find a string or an int in an ArrayCollection, you'll have to do the searching yourself, by some variation of looping or searching. I don't think any of us could tell you why there isn't, say, an optional parameter on that method indicating whether to search by ref or by val, though; so it goes, as they say.
But I'd definitely warn you off monkey-patching the framework code -- that's just asking for trouble. :)
Well, it seems like the ArrayCollection doesn't actually look directly at memory, but only as a last resort. It will attempt to find a Unique ID (UID) for the object. If the UID doesn't exist, it will create one for it using the UIDUtil.as.
You can get around this whole default UID stuff by having your object implement the IUID interface and providing your own UID for the object. The ArrayCollection will look at the UID you provide it.
I would suggest a simple:
in_array($haystack, $arrayCollection->toArray());

Resources