路线改变的奇怪行为
Strange behavior on route change
我在 collection 订阅中遇到了奇怪的流星行为。我有一个抽象路由邮箱,下面有三个路由。收件箱、已发送邮件和 singleMail(显示来自两封邮件的一封邮件的详细信息)。现在的问题是,当我在单个邮件路由中解析邮件 collection 时,它为收件箱和已发送的邮件绑定了相同的邮件 collection,尽管我为两者发布了不同的 collection。
注意:它工作完美,直到我从两者转到单一邮件路由。例如,当我点击收件箱中的任何邮件或发送邮件时,它将转到单一邮件路径并显示电子邮件的详细信息。回来的时候。问题从这里开始。它为两者订阅邮件 collection。即,如果收件箱有 6 个,发送有 3 个,则两者都会显示 9。我试了几个小时,但没有运气。
任何帮助是极大的赞赏 !
我粘贴了很多代码来解决我的确切问题。这是服务器
中的 mail.js
Meteor.publish("mails", function (obj) {
//Will use for admin
isAuth(this);
var query = {identity:this.userId};
if (obj) {
query[obj.condition] = obj.id;
}
return Mails.find(query);
});
Meteor.publish("mailsTo", function (options) {
//if user not logged in
isAuth(this);
Counts.publish(this, 'countOfNewMails', Mails.find({identity: this.userId, 'to.id': this.userId, unread: true}));
Counts.publish(this, 'countOfToMails', Mails.find({identity: this.userId, 'to.id': this.userId}), { noReady: true });
return Mails.find({identity: this.userId, 'to.id': this.userId}, options);
});
Meteor.publish("mailsFrom", function (options) {
//if user not logged in
isAuth(this);
Counts.publish(this, 'countOfFromMails', Mails.find({identity: this.userId, from: this.userId}), { noReady: true });
return Mails.find({identity: this.userId, from: this.userId}, options);
});
这是我的 route.js
angular.module('Secret')
.config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
function($urlRouterProvider, $stateProvider, $locationProvider){
$locationProvider.html5Mode(true);
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'client/home/views/home.ng.html',
controller: 'homeCtrl'
})
.state('login', {
url: '/homes/:id/login',
templateUrl: 'client/login/views/login.ng.html',
controller: 'loginCtrl'
})
.state('signup', {
url: '/homes/:id/signup',
templateUrl: 'client/signup/views/signup.ng.html',
controller: 'signupCtrl'
})
.state('forgotPassword', {
url: '/homes/:id/forgot-password',
templateUrl: 'client/forgotPassword/views/forgotPassword.ng.html',
controller: 'forgotPasswordCtrl'
})
.state('profile', {
url: '/profile',
templateUrl: 'client/profile/views/profile.ng.html',
controller: 'profile'
})
.state('mailbox', {
url: '/mailbox',
templateUrl: 'client/mailBox/views/mailbox.ng.html',
controller: 'mailboxCtrl',
abstract: true
})
.state('mailbox.inbox', {
url: '/inbox',
templateUrl: 'client/inbox/views/inbox.ng.html',
controller: 'inboxCtrl',
resolve: {
"currentUser": ["$meteor", function($meteor){
return $meteor.requireUser();
}]
}
})
.state('mailbox.sent', {
url: '/sent',
templateUrl: 'client/sent/views/sent.ng.html',
controller: 'sentCtrl'
})
.state('mailbox.singleMail', {
url: '/:folder/:id',
params: {
hasnext: null
},
templateUrl: 'client/singleMail/views/single.ng.html',
controller: 'singleCtrl',
resolve: {
"currentUser": ["$meteor", function($meteor){
return $meteor.requireUser();
}],
statesSub: ['$meteor', '$stateParams', function($meteor, $stateParams) {
return $meteor.subscribe('mails', $stateParams.id);
}]
}
})
.state('mailbox.compose', {
url: '/compose/:r/:mailId',
templateUrl: 'client/compose/views/compose.ng.html',
controller: 'composeCtrl'
})
$urlRouterProvider.otherwise("/home");
}])
.run(['$rootScope', '$state',
function($rootScope, $state){
$rootScope.$on("$stateChangeError", function(event, next, previous, error) {
// We can catch the error thrown when the $requireUser promise is rejected
// and redirect the user back to the main page
//if (error === "AUTH_REQUIRED") {
$state.go("home");
//}
});
}]);
并在控制器中作为简单的 SentCtrl
$scope.page = 1;
$scope.perPage = 50;
$scope.sort = { createdAt: -1 };
//get reactively
$meteor.autorun($scope, function() {
$scope.mails = $scope.$meteorCollection(Mails).subscribe('mailsFrom',{
limit: parseInt($scope.getReactively('perPage')),
skip: (parseInt($scope.getReactively('page')) - 1) * parseInt($scope.getReactively('perPage')),
sort: $scope.getReactively('sort')
})
})
InboxCtrl
$scope.page = 1;
$scope.perPage = 50;
$scope.sort = { createdAt: -1 };
$scope.pagechanged = function(sign){
sign ? $scope.page++ : $scope.page--;
}
//get reactively
$meteor.autorun($scope, function() {
$scope.inbox = $scope.$meteorCollection(Mails).subscribe('mailsTo',{
limit: parseInt($scope.getReactively('perPage')),
skip: (parseInt($scope.getReactively('page')) - 1) * parseInt($scope.getReactively('perPage')),
sort: $scope.getReactively('sort')
})
})
在单个邮件中,我只是通过参数接收邮件
$scope.currentMail = $meteor.object(Mails, $stateParams.id);
Whenever you subscribe a collection, you got the subscription handle
for this. So you can stop it when template destroyed. (i.e.
subscription_handle.stop())
on template.destroyed()
in meteor angular
context.
我在 collection 订阅中遇到了奇怪的流星行为。我有一个抽象路由邮箱,下面有三个路由。收件箱、已发送邮件和 singleMail(显示来自两封邮件的一封邮件的详细信息)。现在的问题是,当我在单个邮件路由中解析邮件 collection 时,它为收件箱和已发送的邮件绑定了相同的邮件 collection,尽管我为两者发布了不同的 collection。 注意:它工作完美,直到我从两者转到单一邮件路由。例如,当我点击收件箱中的任何邮件或发送邮件时,它将转到单一邮件路径并显示电子邮件的详细信息。回来的时候。问题从这里开始。它为两者订阅邮件 collection。即,如果收件箱有 6 个,发送有 3 个,则两者都会显示 9。我试了几个小时,但没有运气。 任何帮助是极大的赞赏 ! 我粘贴了很多代码来解决我的确切问题。这是服务器
中的 mail.jsMeteor.publish("mails", function (obj) {
//Will use for admin
isAuth(this);
var query = {identity:this.userId};
if (obj) {
query[obj.condition] = obj.id;
}
return Mails.find(query);
});
Meteor.publish("mailsTo", function (options) {
//if user not logged in
isAuth(this);
Counts.publish(this, 'countOfNewMails', Mails.find({identity: this.userId, 'to.id': this.userId, unread: true}));
Counts.publish(this, 'countOfToMails', Mails.find({identity: this.userId, 'to.id': this.userId}), { noReady: true });
return Mails.find({identity: this.userId, 'to.id': this.userId}, options);
});
Meteor.publish("mailsFrom", function (options) {
//if user not logged in
isAuth(this);
Counts.publish(this, 'countOfFromMails', Mails.find({identity: this.userId, from: this.userId}), { noReady: true });
return Mails.find({identity: this.userId, from: this.userId}, options);
});
这是我的 route.js
angular.module('Secret')
.config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
function($urlRouterProvider, $stateProvider, $locationProvider){
$locationProvider.html5Mode(true);
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'client/home/views/home.ng.html',
controller: 'homeCtrl'
})
.state('login', {
url: '/homes/:id/login',
templateUrl: 'client/login/views/login.ng.html',
controller: 'loginCtrl'
})
.state('signup', {
url: '/homes/:id/signup',
templateUrl: 'client/signup/views/signup.ng.html',
controller: 'signupCtrl'
})
.state('forgotPassword', {
url: '/homes/:id/forgot-password',
templateUrl: 'client/forgotPassword/views/forgotPassword.ng.html',
controller: 'forgotPasswordCtrl'
})
.state('profile', {
url: '/profile',
templateUrl: 'client/profile/views/profile.ng.html',
controller: 'profile'
})
.state('mailbox', {
url: '/mailbox',
templateUrl: 'client/mailBox/views/mailbox.ng.html',
controller: 'mailboxCtrl',
abstract: true
})
.state('mailbox.inbox', {
url: '/inbox',
templateUrl: 'client/inbox/views/inbox.ng.html',
controller: 'inboxCtrl',
resolve: {
"currentUser": ["$meteor", function($meteor){
return $meteor.requireUser();
}]
}
})
.state('mailbox.sent', {
url: '/sent',
templateUrl: 'client/sent/views/sent.ng.html',
controller: 'sentCtrl'
})
.state('mailbox.singleMail', {
url: '/:folder/:id',
params: {
hasnext: null
},
templateUrl: 'client/singleMail/views/single.ng.html',
controller: 'singleCtrl',
resolve: {
"currentUser": ["$meteor", function($meteor){
return $meteor.requireUser();
}],
statesSub: ['$meteor', '$stateParams', function($meteor, $stateParams) {
return $meteor.subscribe('mails', $stateParams.id);
}]
}
})
.state('mailbox.compose', {
url: '/compose/:r/:mailId',
templateUrl: 'client/compose/views/compose.ng.html',
controller: 'composeCtrl'
})
$urlRouterProvider.otherwise("/home");
}])
.run(['$rootScope', '$state',
function($rootScope, $state){
$rootScope.$on("$stateChangeError", function(event, next, previous, error) {
// We can catch the error thrown when the $requireUser promise is rejected
// and redirect the user back to the main page
//if (error === "AUTH_REQUIRED") {
$state.go("home");
//}
});
}]);
并在控制器中作为简单的 SentCtrl
$scope.page = 1;
$scope.perPage = 50;
$scope.sort = { createdAt: -1 };
//get reactively
$meteor.autorun($scope, function() {
$scope.mails = $scope.$meteorCollection(Mails).subscribe('mailsFrom',{
limit: parseInt($scope.getReactively('perPage')),
skip: (parseInt($scope.getReactively('page')) - 1) * parseInt($scope.getReactively('perPage')),
sort: $scope.getReactively('sort')
})
})
InboxCtrl
$scope.page = 1;
$scope.perPage = 50;
$scope.sort = { createdAt: -1 };
$scope.pagechanged = function(sign){
sign ? $scope.page++ : $scope.page--;
}
//get reactively
$meteor.autorun($scope, function() {
$scope.inbox = $scope.$meteorCollection(Mails).subscribe('mailsTo',{
limit: parseInt($scope.getReactively('perPage')),
skip: (parseInt($scope.getReactively('page')) - 1) * parseInt($scope.getReactively('perPage')),
sort: $scope.getReactively('sort')
})
})
在单个邮件中,我只是通过参数接收邮件
$scope.currentMail = $meteor.object(Mails, $stateParams.id);
Whenever you subscribe a collection, you got the subscription handle for this. So you can stop it when template destroyed. (i.e.
subscription_handle.stop())
ontemplate.destroyed()
in meteor angular context.