【发布时间】:2016-05-25 19:19:34
【问题描述】:
我正在使用 angular2-meteor,我已经在使用 pure: false。但是管道有时会运行,有时不会。有关问题的详细信息,请参阅代码中的我的 cmets。
谢谢
<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) If I only do these two lines, the change of other users' status can show on the screen immediately.
// let users = usersCursor.fetch();
// return users;
// (2) If sort users by status, the page sometimes updates, sometimes not when user status change.
// If not update automatically, I click that part of screen, it will update then.
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;
}
}
【问题讨论】:
-
什么是 usersCursor.fetch?是异步调用吗?
-
@pixelbits 很抱歉不清楚,只是更新。它基本上从数据库中获取所有用户
标签: meteor typescript angular angular2-meteor