Utilizing angular2-meteor, I have already implemented pure: false
. However, the pipe seems to be running inconsistently. For more details on the issue, please refer to my comments within the code.
Thank you.
<div *ngFor="#user of (users|orderByStatus)">
{{user.status.online}}
</div>
users:Mongo.Cursor<Meteor.User>;
ngOnInit()
{
this.subscribe('users', () => {
this.autorun(() => {
this.users = Meteor.users.find();
});
}, true);
}
import {Pipe} from 'angular2/core';
@Pipe({
name: 'orderByStatus',
pure: false
})
export class OrderByStatusPipe {
transform(usersCursor:Mongo.Cursor<Meteor.User>):Array<Meteor.User> {
console.log("OrderByStatusPipe runs");
// (1) By only including these two lines, changes in other users' status are immediately reflected on the screen.
// let users = usersCursor.fetch();
// return users;
// (2) When sorting users by their status, the page sometimes updates automatically and sometimes it does not reflect changes.
// To trigger an update manually, I need to click on that section of the screen.
let users:Array<Meteor.User> = usersCursor.fetch();
users.sort((a, b) => {
return (a.status.online === b.status.online) ? 0 : (a.status.online ? -1 : 1);
});
return users;
}
}