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.
Related
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?
I have one observable array in my store that is deeply nested. Lets called the property people (which contains elements of class Person). Now the Person class has a property products which is an array of Product, a class which also has nesting through ShippingInformation and so on. When the app is loaded I get the people array with all nested data. Now upon any data update I receive the updated peoples array again from the server. A lot of the times the elements in the people array are the same, the only change being some ShippingInformation.
Now how should I update the existing observable array people? I do not want to reassign it because often times the direct Person elements are the same. It is only some nested information on one Person that has changed. Is there a way that I can sync the two arrays without changing the ref on the array with a reassign so that I avoid major rerenders in react? Can mobx help me here or are there any other lib I can use to help me update without triggering unnecessary rerenders? If not, are these rerenders that bad, performance wise, I guess react must go through the diffing process at least
I have an API that gives back a single JSON Object if there is only one element in the response and gives back an array of objects if there is more than one element in the response.
I could use the javascript controller to simply make it always an array even with just one object in it, and loop through the array with ng-repeat, but the problem is that I have the same issue in some properties of the JSON object too.
I donĀ“t want to change the JSON structure of the data and I might need them to update the record via the API, so I was looking for a way to handle this in Angular 1.x.
Is there a simple way in angular to display the data regardless if it is only one object or an array of objects?
There is no simple way to do that. It is simpler if you transform the one object into array in your controller:
getSomething(...).then(res => res.length ? res : [res])
.then(resArr => $scope.somethings = resArr);
I think you will have to modify the response you're getting back. Otherwise you will have to do a lot of awkward, case-by-case checks scattered all over your view logic.
You will probably need a service layer between your controllers and the API which "translates" the JSON data. So, when requesting data, it will loop through the response recursively and wrap all the lone objects in arrays.
Then, when posting data to the API, it will loop back through again and remove the array wrappers from lone objects.
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.
I need to maintain a list of blocks in JavaScript and render them in a specific order. There could be add , delete update operations on the list. This list could hold up to 1000 objects.
At the time of render, the list should be rendered in a specific order.
I am looking for the best way to implement n maintain order/sorted list in JavaScript. I need to perform the following operations on this list : Add delete; update and search.
Is there any Jquery or any other library available that can do this?
Thanks for you help.
You could implement your own Linked List in Javascript very easily.
What you need is a simple sort-lookup table. Assuming the elements have unique IDs, use something like this:
var myList = []
myList[position] = elementId
Then you manage the array, not the elements. Read out the array in its native order and write your HTML from that. The rest is simple array manipulation.
Backbone.js might get you there. It has models, collections, and views. You can specify a sort function for collections, which keeps the collection's list of models sorted. The collection emits signals for create, update, and delete events. You can then write a view that renders the collection of models in sorted order when any of the create, update, or delete events are emitted. The models don't necessarily have to correspond to server-side models.