路线变化的奇怪行为
Posted
技术标签:
【中文标题】路线变化的奇怪行为【英文标题】:Strange behavior on route change 【发布时间】:2015-12-30 08:23:12 【问题描述】:我在收藏订阅时遇到了奇怪的流星行为。我有一个抽象路由邮箱,下面有三个路由。收件箱、已发送和 singleMail(显示来自两者的一封邮件的详细信息)。现在的问题是,当我在单个邮件路由中解决邮件集合时,它为收件箱和已发送的邮件集合绑定了相同的邮件集合,尽管我为两者发布了不同的集合。 注意:它工作完美,直到我从两者都转到单一邮件路线。例如,当我单击收件箱中的任何邮件或发送时,它将转到单个邮件路由并显示电子邮件的详细信息。回来的时候。问题从这里开始。它为两者订阅邮件集合。即如果收件箱有 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')
)
)
收件箱控件
$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);
【问题讨论】:
【参考方案1】:每当您订阅一个集合时,您都会获得订阅句柄 为了这。所以你可以在模板被破坏时停止它。 (IE。
subscription_handle.stop())
ontemplate.destroyed()
在流星角 上下文。
【讨论】:
以上是关于路线变化的奇怪行为的主要内容,如果未能解决你的问题,请参考以下文章