I have an object and a temp object now if i do
tempObj = obj
and change stuff in tempObj they changes have an effect on obj is there a way i can stop it from doing this?
Regards
Mark
This is a standard behavior in many languages. When you do tempObj = obj you are NOT creating a duplicate object. You are creating another reference to the same object.
I don't think you can change this behavior, and certainly I don't think you should :)
What you need is creating another object, a duplicate of the original object. You can implement a function to do that. Maybe this can help
http://blog.comtaste.com/2007/10/improving_object_copy.html
Good luck!
What you are doing is making a reference to the original object not a copy of the original. You should create a deep copy of your object. It seems that someone already wrote the steps to do so...
http://www.as3dp.com/2008/09/23/actionscript-30-clone-a-prelude-to-the-prototype-design-pattern/
Hope this helps
Related
I have an object in my database following a file upload that look like this
a:1:{s:4:"file";a:3:{s:7:"success";b:1;s:8:"file_url";a:2:{i:0;s:75:"http://landlordsplaces.com/wp-content/uploads/2021/01/23192643-threepersons.jpg";i:1;s:103:"http://landlordsplaces.com/wp-content/uploads/2021/01/364223-two-female-stick-figures.jpg";}s:9:"file_path";a:2:{i:0;s:93:"/var/www/vhosts/landlordsplaces.com/httpdocs/wp-content/uploads/2021/01/23192643-threepersons.jpg";i:1;s:121:"/var/www/vhosts/landlordsangel.com/httpdocs/wp-content/uploads/2021/01/364223-two-female-stick-figures.jpg";}}}
I am trying with no success to parse extract the two jpg urls programmatically from the object so i can show the images ont he site. Tried assigning parse(object) but that isnt helping. I just need to get the urls out.
Thank you in anticipation of any general direction
What you're looking at is not a JSON string. It is a serialized PHP object. If this database entry was created by Forminator, you should use the Forminator API to retrieve the needed form entry. The aforementioned link points to the get_entry method, which I suspect is what you're looking for (I have never used Forminator), but in any case, you should look for a method that will return that database entry as a PHP object containing your needed URLs.
In case it is ever of any help to anyone the answer to the question was based on John input. The API has the classes to handle that without needing to understand the data structure.
Forminator_API::initialize();
$form_id = 1449; // ID of a form
$entry_id = 3; // ID of an entry
$entry = Forminator_API::get_entry( $form_id, $entry_id );
$file_url = $entry->meta_data['upload-1']['value']['file']['file_url'];
$file_path = $entry->meta_data['upload-1']['value']['file']['file_path'];
var_dump($entry); //contains paths and urls
Hope someone benefits.
I have an object in localStorage, something like:
{"one":"oneone","two":"twotwo"}
You get the picture.
For some reason attempting to add more items to the object won't work. If I want to add "three":"threethree", it ignores that and when I update the storage area in Opera Dragonfly it shows the same old object, unchanged...
Does anybody know how to append to objects on the fly? Or will I have to append to a copy of the object in my code, then clear the old object from localStorage, and store the updated one? I can see this being the only way, but it seems like it might be a bit ugly... Ideas please! :D
I think that the safest and cleanest way to do this is getting the object from localStorage, parsing it, add some data ans stringify it for storing.
Like you say, it is a bit ugly, but you can create a function to do this
function mergeLocalStorage(key, obj){
var newObj = JSON.parse(localStorage.getItem(key));
for (var k in obj){
newObj[k] = obj[k];
}
localStorage.setItem(key, JSON.stringify(newObj));
return newObj;
}
sometimes we need clone a object.but if a displayObject has some children and use the function like this:
function clone(source:*):*
{
var b:ByteArray = new ByteArray();
b.writeObject(source);
b.position = 0;
return(b.readObject());
}
but the result has no children.. .. . so what should I do ?
Unfortunately automatic cloning of objects in actionscript is a waste of time in the majority of cases.
Your snippet is right, but serialization/deserialization via ByteArray cannot perform real deep copy, i.e. copying of all references and containers.
ByteArray technique will work only with non-reference data types (Number, int, String, etc.)
So there is no silver bullet and only one adequate solution - to write the clone() method for your class manually.
I didn't have to program a clone-method myself yet, but i found a way that might do the trick. By iterating through all your variables (in an xml-representation), you can copy them in a new instance of your class.
you can find the method i am talking about on this link: http://www.learnosity.com/techblog/index.cfm/2008/2/6/AS3--Looping-over-properties-of-a-class
Let me know if it works, i'm kind of curious myself :)
For a game I'm attempting to develop, I am writing a resource pool class in order to recycle objects without calling the "new" operator. I would like to be able to specify the size of the pool, and I would like it to be strongly typed.
Because of these considerations, I think that a Vector would be my best choice. However, as Vector is a final class, I can't extend it. So, I figured I'd use composition instead of inheritance, in this case.
The problem I'm seeing is this - I want to instantiate the class with two arguments: size and class type, and I'm not sure how to pass a type as an argument.
Here's what I tried:
public final class ObjPool
{
private var objects:Vector.<*>;
public function ObjPool(poolsize:uint, type:Class)
{
objects = new Vector.<type>(poolsize); // line 15
}
}
And here's the error I receive from FlashDevelop when I try to build:
\src\ObjPool.as(15): col: 18 Error: Access of undefined property type.
Does anybody know of a way to do this? It looks like the Flash compiler doesn't like to accept variable names within the Vector bracket notation. (I tried changing constructor parameter "type" to String as a test, with no results; I also tried putting a getQualifiedClassName in there, and that didn't work either. Untyping the objects var was fruitless as well.) Additionally, I'm not even sure if type "Class" is the right way to do this - does anybody know?
Thanks!
Edit: For clarification, I am calling my class like this:
var i:ObjPool = new ObjPool(5000, int);
The intention is to specify a size and a type.
Double Edit: For anyone who stumbles upon this question looking for an answer, please research Generics in the Java programming language. As of the time of this writing, they are not implemented in Actionscript 3. Good luck.
I have been trying to do this for a while now and Dominic Tancredi's post made me think that even if you can't go :
objects = new Vector.<classType>(poolsize);
You could go something like :
public final class ObjPool
{
private var objects:Vector.<*>;
public function ObjPool(poolsize:uint, type:Class)
{
var className : String = getQualifiedClassName(type);
var vectorClass : Class = Class(getDefinitionByName("Vector.<" + className + ">"));
objects = new vectorClass(poolsize);
}
}
I tried it with both int and a custom class and it seems to be working fine. Of course you would have to check if you actually gain any speed from this since objects is a Vector.<*> and flash might be making some implicit type checks that would negate the speed up you get from using a vector.
Hope this helps
This is an interesting question (+1!), mostly because I've never tried it before. It seems like from your example it is not possible, which I do find odd, probably something to do with how the compiler works. I question why you would want to do this though. The performance benefit of a Vector over an Array is mostly the result of it being typed, however you are explicitly declaring its type as undefined, which means you've lost the performance gain. So why not just use an array instead? Just food for though.
EDIT
I can confirm this is not possible, its an open bug. See here: http://bugs.adobe.com/jira/browse/ASC-3748 Sorry for the news!
Tyler.
It is good you trying to stay away from new but:
Everything I have ever read about Vector<> in actionscript says it must be strongly typed. So
this shouldn't work.
Edit: I am saying it can't be done.
Here see if this helps.
Is it possible to define a generic type Vector in Actionsctipt 3?
Shot in the dock, but try this:
var classType:Class = getDefinitionByName(type) as Class;
...
objects = new Vector.<classType>(poolsize); // line 15
drops the mic
I don't really see the point in using a Vector.<*>. Might as well go with Array.
Anyhow, I just came up with this way of dynamically create Vectors:
public function getCopy (ofVector:Object):Object
{
var copy:Object = new ofVector.constructor;
// Do whatever you like with the vector as long as you don't need to know the type
return copy;
}
Would it be possible to serialize a model object into a query string?
I've no idea if this is even possible, but if not, what is the best way to do this?
<% Html.RenderAction("Grid", "Grid", new { gridModel= ViewData["model"]}); %>
The Model is containing ca 20 properties, and creating the querystring with them in individually would make it a pain to work with and it would look really ugly. so what alternatives do I have?
To answer directly, you could use something like JSON.NET to serialize it to ASCII and then base64 encode it.
However, there are very real limits on how much data you can include in the query string and I'd hesitate to do this.
The closest thing I can think of would be to create a GUID, use that GUID as a key to store the object in Session, then pass the RenderAction the GUID. That action would pull the object out of the Session and then remove it from the Session.
Quick pseudocode (not guaranteed to even compile, much less work)
var _requestKey = Guid.NewGuid();
Session[requestKey] = gridModel;
Then on the other side:
var gridModel = Session[requestKey] as GridModelType;
Session[requestKey] = null;
I ended up using Form for this. Wasn't as pretty and nice as I wanted, but as far as I understand the only good way I could do it.