首页 > angular js 数据建模

angular js 数据建模

(function() {
    'use strict';
    angular
        .module('app')
        .factory('User', User);

    //用户模型
    User.$inject = ['$http', '$cookieStore'];

    function User($http, $cookieStore) {
        function User(userData) {
            if (userData) {
                this.setData(userData);
            }
        };
        User.prototype = {
            setData: function(userData) {
                angular.extend(this, userData);
            },
            login: function(user) {
                var scope = this;
                $http.post('/user/login', user).success(function(data) {
                    scope.setData(data);
                }).error(function(data) {
                    scope.setData(data);
                });
                return this;
            },
            register: function(user) {
                var scope = this;
                $http.post('/user/register', user).success(function(data) {
                    scope.setData(data);
                }).error(function(data){
                    scope.setData(data);
                });
                return this;
            },
            delete: function() {
                $http.delete('ourserver/books/' + userId);
            },
            update: function(id) {
                var scope = this;
                $http.put('/user/update/' + id, this).success(function(data) {
                    scope.setData(data);
                }).error(function(data) {
                    scope.setData(data);
                });
                return this;
            },
            getImageUrl: function(width, height) {
                return 'our/image/service/' + this.user.id + '/width/height';
            },
            isAvailable: function() {
                if (!this.user.stores || this.user.stores.length === 0) {
                    return false;
                }
                return this.user.stores.some(function(store) {
                    return store.quantity > 0;
                });
            }
        };
        return User;
    }
})();

封装user为一个service,方便调用,但是每个请求的success和error方法返回的结果改怎么取,怎么设计比较好?


用 promise 有空填坑

【热门文章】
【热门文章】