我试图创建视图/查询数据库......但没有任何反应......

时间:2016-06-22 00:49:35

标签: angularjs pouchdb

app.controller('AdminUserCtrl', function ($scope, $controller, $location, $http, $rootScope, $pouchDB, $state, $stateParams) {
    $controller('AdminCtrl', {$scope: $scope});
    console.log("AdminUser Controller reporting for duty.");

    $scope.items = {};

    $pouchDB.startListening();

    // try to call
    $pouchDB.getUsers();

    console.log($pouchDB.getUsers());


    // Listen for changes which include create or update events
    $rootScope.$on("$pouchDB:change", function (event, data) {
        $scope.items[data.doc._id] = data.doc;
        $scope.$apply();
    });

    // Listen for changes which include only delete events
    $rootScope.$on("$pouchDB:delete", function (event, data) {
        delete $scope.items[data.doc._id];
        $scope.$apply();
    });

    // Look up a document if we landed in the info screen for editing a document
    if ($stateParams.documentId) {
        $pouchDB.get($stateParams.documentId).then(function (result) {
            $scope.inputForm = result;
        });
    }
app.service("$pouchDB", ["$rootScope", "$q", function ($rootScope, $q) {

        var database;
        var changeListener;
        this.setDatabase = function (databaseName) {
            database = new PouchDB(databaseName);
        };
        this.getUsers = function () {
            return  database.query({
                map: function (doc, emit) {
                    if (doc.type === "user") {
                        emit(doc._id, doc);
                    }
                }
            });
        };
        this.startListening = function () {
            changeListener = database.changes({
                live: true,
                include_docs: true
            }).on("change", function (change) {
                if (!change.deleted) {
                    $rootScope.$broadcast("$pouchDB:change", change);
                } else {
                    $rootScope.$broadcast("$pouchDB:delete", change);
                }
            });
        };

我尝试创建视图/查询数据库......但没有任何反应......

任何人都可以提供如何在angularjs-pouchdb中创建视图的示例吗?

的console.log($ pouchDB.getUsers());

返回:

  

承诺{[[PromiseStatus]]:"待定",[[PromiseValue]]:undefined}

1 个答案:

答案 0 :(得分:1)

按照文档,你想要的东西看起来像。 。

app.service("$pouchDB", ["$rootScope", "$q", function ($rootScope, $q) {
    var instance = {};
    instance.database = null;
    instance.changeListener = null;
    instance.setDatabase = function (databaseName) {
        this.database = new PouchDB(databaseName);
    };
    instance.getUsers = function () {
        return this.database.query({
            map: function (doc, emit) {
                if (doc.type === "user") {
                    emit(doc._id, doc);
                }
            }
        });
    };
    instance.startListening = function () {
        this.changeListener = this.database.changes({
            live: true,
            include_docs: true
        }).on("change", function (change) {
            if (!change.deleted) {
                $rootScope.$broadcast("$pouchDB:change", change);
            } else {
                $rootScope.$broadcast("$pouchDB:delete", change);
            }
        });
    };
    return instance; 
}]);