I'm using Angular2-meteor which is using Angular2 Beta 1 (as of right now).
I have a simple component containing:
- Button to add a document. The new document is displayed with a button to remove it by its _id.
- There's also a "Remove All" button which loops through the collection.find() removing each document by _id.
It mostly works fine. You can add documents and remove them with their individual remove button. When you "Remove All" it removes them all from the database. The cursor reports a count() of 0. A new collection.find().count() reports 0. But it only removes the first document which is displayed on the template by the *ngFor on the client that initiated the removeAll()
. The other documents still show in the browser. When you reload the page, it displays the the correct contents of the database. Other connected clients always display the correct contents of the collection. Only the client that initiates the removeAll()
is affected.
The template, a "Remove All" button and an *ngFor displaying the documents
<input type="button" value="Remove All" (click)="removeAll()">
<ul>
<li *ngFor="#doc of docs">
<input type="button" value="Remove" (click)="remove(doc._id)">
point: x={{ doc.x }} y={{ doc.y }} _id={{ doc._id }}
</li>
</ul>
The component:
@Component({
selector: 'db-test',
templateUrl: 'client/db-test/db-test.html',
directives: [CORE_DIRECTIVES],
})
export class DbTestComponent{
coll = DbTestCollection;
docs: Mongo.Cursor<Object>;
constructor() {
this.docs = this.coll.find();
}
removeAll() {
this.docs.forEach((d) => {
this.coll.remove(d._id);
});
}
remove(id) {
this.coll.remove({ _id: id });
}
add(point: Point = {x: Math.random(), y: Math.random()}) {
this.coll.insert(point);
}
}