meteor - 路线变化的奇怪行为

标签 meteor angular-meteor

我在收藏订阅中面临奇怪的 meteor 行为。我有一个抽象路由邮箱,下面有三个路由。收件箱、已发送和 singleMail(显示来自两者的一封邮件的详细信息)。现在的问题是,当我在单个邮件路由中解决邮件集合时,它为收件箱和已发送的邮件集合绑定(bind)了相同的邮件集合,尽管我为两者发布了不同的集合。
注意:它工作完美,直到我从两者都转到单一邮件路线。例如,当我单击收件箱中的任何邮件或发送时,它将转到单个邮件路线并显示电子邮件的详细信息。回来的时候。问题从这里开始。它为两者订阅邮件集合。即如果收件箱有 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");
            //}
        });
    }]);

在 Controller 中作为简单的 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);

最佳答案

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.

关于meteor - 路线变化的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32901840/

相关文章:

javascript - 使用 ng-href 查看 ng-repeat 中包含具有 objectId 的集合的示例详细信息 View

angularjs - 在新数据到达 Meteor 时对其进行动画处理

javascript - meteor 集合不会在刷新时加载

node.js - 在 WebStorm 11 中创建 angle-meteor 项目

node.js - Windows 上的 Meteor 设置问题

javascript - 在 Meteor 中使用 fs 模块获取 Uncaught TypeError _fs2.default.readFile 不是函数

jquery - 在 MeteorJS 中为表格行制作动画

javascript - 在 Meteor 中控制用户访问

javascript - Meteor + CSS 无法正确加载

javascript - 如何在 meteor 应用程序上使用 X 射线?