Delete Object from Array and destroy it completely - javascript

I have an array of Objects (ES6 Class I created). When I delete this objects from within the array I store them, it seems like it isnt fully "destroyed". These Object classes have websocket connections to external APIs and even when I remove these Objects from my Object Array using splice (which works) I still see the websocket events coming in for them.
How can I really destroy the whole Instance of the object when I remove it from my array?

Related

Javascript reference object by an identifier or reference it directly?

I am creating quite many objects in Javascript, which I need to be able to reference to retrieve later, for example through an array. Now I am wondering how to save the object reference in the array.
The objects will interract with an external library, which can function either with object IDs or the object itself. I know Javascript treats object variables like pointers and not object copies, so is it safe to reference the objects directly ?
At first I was thinking "if I save the IDs of the objects, that would keep memory usage low", but is this true ? How much memory does an object variable* consume versus an integer variable ?
*the pointer to the object, not the object data
Also, if saving the objects directly is the way to go, is it ok to use array.push() to add the new object to the array ? Is array.push() creating a copy or does it work like a variable ?
Yes, you can reference the objects directly. If you really care about memory usage, I'd say the way of passing the ids will use more memory since you'll have to maintain another set of mappings.
Passing the objects directly will let the JavaScript engine to work directly with the object references. That's not affecting the memory usage, as long you don't to any cloning.
I believe the relevant question is weather or not saving an integer and using that as an identifier later is more effective than saving a reference to the object.
I'd go with the reference in an array. the size of a reference vs a number is going to be negligible.

Using splice with Breeze without setting linked properties to null

For some reordering code I call splice on an array of Breeze entities. Generally this works fine, but on removing an entity using splice from the array, its navigation properties are set to null. After adding the same entity back into the same array on a different position, navigation property seems to be restored, but the entityState has already changed to modified.
The responsible code seems to be this call.
Is there a way to move an entity in an array of entities to a different position without having the entityState to be changed?
As responded by in a GitHub issue: Workaround is to use a temporary standard array instead of an observable array, process the arrays using splice in the temporary array and write them back to the observable array.

How to post a Json object for an object of a class with an overloaded constructor?

I have a view in my web application that contain some listboxes etc. I want to post the values of these elements to my MVC controller. In order to do so, I created a class that contains all neccessary data. This class has two constructors: one without any parameters (creates an empty object) and one with two parameters (pre-populates the object with data from a DB).
Now, when I create the Json object according to the class, it gets sent, but the server responds with error 500 and tells me that a System.MissingMethodException occured because there has not been defined a parameterless constructor for this object. Well... there surely is one, but obviously the other one (the "parameterful") gets called.
Is there a way to tell my application which constructor to use when deserializing the Json string into an object?
It turned out that I overlooked one of the sub-objects within my object, this class indeed only had a constructor with parameters. Everything works now.
BTW: It would be way more helpful if the error messages would cleary state which object it is talking about. The same applies for error messages concerning uninitialized objects.

Is it possible to store an object with functions (proto) as a cookie?

I have an object called Game, which has properties name, number, teams, etc. Many of its properties are objects themselves: teams is a list of Team objects.
All of these objects have functions defined using proto (not sure of the actual terminology), for example
Game.prototype.getName() {
eturn this.name;
}
is one of the game's functions. I want to be able to store the game, along with its functions, as a cookie. I should be able to load this cookie later on and get my original Game object.
JSON.stringify doesn't preserve the functions, just the properties of the Game. Is there anyway I can 'stringify' an entire object including its JavaScript functions? I'm using AngularJS if that matters.
I think you should only serialize data, not the behaviour. Once retrieving your data back from JSON you could instanciate relevant Objects with that data, thus giving them back their behaviour.

Can / should I call firebaseRef.set passing an array?

The question title says it all. Here are a couple related pages from the Firebase docs:
writing data
managing lists
Is there a better way to update all the items in an "array" on Firebase?
Note that using .push on a ref in firebase does not append to an array but rather an array-like object with non-ordinal keys that look like -JH1w9H0qPJIFu_OF_JO. If you want to work with actual JavaScript arrays, you need to treat them as a unit. That is, any time you update a property that contains an array you have to set the entire array -- you can't use Firebase to update individual components of the array.
If you need ordered data, use priorities.
That being said, it is safe to write arrays ([]) as properties in Firebase. If you have a list and you need to update each property of the list, you cycle through the snapshot (once you've retrieved it from the ref using .value or the like) using .forEach. You can then us .update on each individual child.

Categories

Resources