Insert am document at index in Meteor Collection - collections

i need to replace an elements in an Meteor.Collection, but want to replace it, so it appears in the same position as the old one.
For that i found in the docs in observe, that there is an atIndex parameter in the callbacks.
e.g.
cursor.observe({
addedAt: function(document, atIndex, before){
...
}
...
})
does this mean i can insert objects at a specific index position, if so how?

If you use .update() it will appear in the same position.
Or maybe I misunderstand what you're trying to accomplish?

Related

How to remove specific element from array with RactiveJs

I need to remove a specific element from the array (either by index or by object itself - doesn't matter). Seems like I'm blind but I don't see appropriate method in RactiveJs documentation. I found a method to remove an element from the beginning of the arrya (shift) or from the end of the array (pop) but don't see a method to remove specific element.
Seems like ractive.splice
ractive.splice('myArray', indexToRemove, 1)
is the way to go.
Define index reference on list (the num in below example) and then use splice:
{{#each students:num}}
<p>{{firstName}}</p>
<button on-click="event.splice('..', event.get('num'), 1)">Remove</button>
{{/each}

Set an Observer Key in JSViews

Basically can an observer object's key be rename in JSViews?
This is related to stack question and my updated JSFiddle example,
in which I rename files where the file name is the object property as well as the key.
My real world example is actually using an onBeforeChange helper to perform the rename which is provided the arguments: oldValue & value.
Then I use the oldValue to navigate through the observer object to rename. But, because I don't rename the object key as well further renames will fail because the oldValue is now out of sync.
I hope that explanation plus the above fiddle makes sense... :s
Thanks for you consideration!
You can achieve something close to that by calling:
$.observable(object).setProperty("newKey", object.key);
$.observable(object).setProperty("key", undefined);
That will leave a property object.key with the value undefined, but will not actually remove the property.
If you want you can then call
delete object.key;
There will probably be a new $(object).removeProperty("someKey") in an upcoming update - which will allow you to write:
$.observable(object).setProperty("newKey", object.key);
$.observable(object).removeProperty("key");
UPDATE
You can now use removeProperty:
$.observable(object).removeProperty("key");

What happens to QVector's item when it's deleted elsewhere?

I'm wondering about what happens when I delete a QVector's item?
Is it automatically removed from the
QVector?
Do I have to remove it manually?
Also, how can I find out the index of an iteration of the iterator?
Best regards
If you have a QVector<Thing*> and delete one of the Things that stored in it, it will not be removed automatically from the vector. You need to do that yourself.
As far as I know, and from what I read in the docs, none of the QVector iterators has a method to tell at what index it is positioned.
But if you have a reference to the vector itself (or at least to it's begin() iterator), you can use:
int position = iter - v.begin();

removing an item from a filtered ArrayCollection

I'm having some issues when calling getItemIndex on an ArrayCollection with a filterFunction set.
I do something like myAC.removeItemAt(myAC.getItemIndex(myObject)), which works fine when the filtering hasn't been applied. As soon as filtering is applied, getItemIndex seems to return -1 in every case.
Has anyone come across this before? What the best way to remove an item form a filtered ArrayCollection?
Thanks a lot.
Evan
What exactly is your filter filtering out? If you've filtered out everything, getItemIndex should return -1.
Are you hoping to remove items that are still visible when your filter has been applied? If you still want to remove an item that's filtered out, you could temporarily disable the filter:
var filter:Function = ac.filterFunction;
ac.fiterFunction = null;
ac.refresh();
// remove item
ac.filterFunction = filter;
ac.refresh();
I think you'll find there is a source object within the ArrayCollection. What you are seeing is a view of the underlying data with a sort or filter applied. You really want to delete from the underlying source object.
Any time I've dealt with adding and removing items from ArrayCollections in Flex, I've always kept a copy of the original ArrayCollection. Any adding or removing of items happen to that original copy.
Once the changes have been made to the original, I move those forward to the filtered list.
Remove it from source directly
arrayCollection.source.splice(i, 1)
Yeah, so I did find out that I was changing the property of the object - to one that would have it filtered out - prior to trying to remove it. Of course I would get -1 in that case. My mistake.
Ended up going with your suggestion, Stiggler. Seems to work fine, though it seems like there should be a less hackish way to handle this type of thing. Perhaps a parameter you could pass to removeItemAt that would let you access the unfiltered collection.
Anyway, thanks to both of you for your responses. Much appreciated.

Deleting items in foreach

Should you be allowed to delete an item from the collection you are currently iterating in a foreach loop?
If so, what should be the correct behavior?
I can take quite a sophisticated Collection to support enumerators that track changes to the collection to keep position info correct. Even if it does some compromisation or assumptions need to be made. For that reason most libraries simply outlaw such a thing or mutter about unexpected behaviour in their docs.
Hence the safest approach is to loop. Collect references to things that need deleting and subsequently use the collected references to delete items from the original collection.
It really depends on the language. Some just hammer through an array and explode when you change that array. Some use arrays and don't explode. Some call iterators (which are wholly more robust) and carry on just fine.
Generally, modifying a collection in a foreach loop is a bad idea, because your intention is unknown to the program. Did you mean to loop through all items before the change, or do you want it to just go with the new configuration? What about the items that have already been looped through?
Instead, if you want to modify the collection, either make a predefined list of items to loop through, or use indexed looping.
Some collections such as hash tables and dictionaries have no notion of "position" and the order of iteration is generally not guaranteed. Therefore it would be quite difficult to allow deletion of items while iterating.
You have to understand the concept of the foreach first, and actually it depends on the programming language. But as a general answer you should avoid changing your collections inside foreach
Just use a standard for loop, iterate through the item collection backwards and you should have no problem deleting items as you go.
iterate in reverse direction and delete item one by one... That should proper solution.
No, you should not. The correct behaviour should be to signal that a potential concurrency problem has been encountered, however that is done in your language of choice (throw exception, return error code, raise() a signal).
If you modify a data structure while iterating over its elements, the iterator might no longer be valid, which means that you risk working on objects that are no longer part of the collection. If you want to filter elements based on some more complex notation, you could do something like this (in Java):
List<T> toFilter = ...;
List<T> shadow;
for ( T element : toFilter )
if ( keep(element) )
shadow.add(element);
/* If you'll work with toFilter in the same context as the filter */
toFilter = shadow;
/* Alternatively, if you want to modify toFilter in place, for instance if it's
* been given as a method parameter
*/
toFilter.clear();
toFilter.addAll(shadow);
The best way to remove an item from a collection you are iterating over it to use the iterator explitly. For example.
List<String> myList = ArrayList<String>();
Iterator<String> myIt = myList.iterator();
while (myIt.hasNext()) {
myIt.remove();
}

Resources