简体   繁体   中英

Angular 1.x clone service

For example i have service like this:

services.User=function(){
    var self=this;
    self.obj = {
        _id: "",
        fullname: "",
        email: "",
    }
    self.save = function(user){
        self.obj._id=user._id;
        self.obj.fullname=user.fullname;
        self.obj.email=user.email;
    }
}

and i need service or factory which will extend this. I have tried this:

factories.MyUser=function(User){
    User.save({
        _id: '123',
        fullname: 'Honchar Denys',
        email: 'crackeraki@gmail.com'
    });
    return User;
}

This didn't extend it, but use it and if i call something like this:

factories.MyPartner=function(User){
    User.save({
        _id: '234',
        fullname: 'Honchar Maria',
        email: 'margood1990@gmail.com'
    });
    return User;
}

One of two factories will rewrite other. Service.User is called only once, how can i make it duplicate for each time something call it. The end result is that MyUser and MyPartner injection in controllers have the same .obj object. Is there a way to angular.copy(factoryInstance) ?

Creating Multiple Instances from an AngularJS Factory

var app = angular.module("myApp",[]);
app.factory("MyPartner", function($http){
    function newPartner () {
        var myParter = {};
        var promise = $http.get('/api/user/myPartner');

        myPartner.$promise = promise
          .then ( function onFulfilled(response) {
            angular.merge(myPartner, response.data);
            return myPartner;
        });
        return myPartner;
    };
    return newPartner; 
});

It is important to realize that invoking the MyPartner.newPartner function immediately returns an empty reference. Once the data is returned from the server the existing reference is populated with the actual data.

The MyPartner object has this additional property:

  • $promise : the promise of the original server interaction that created this instance.

On success, the promise is resolved with the same resource instance or collection object, updated with data from server. This makes it easy to use in resolve section of $routeProvider.when() to defer view rendering until the resource is loaded.

On failure, the promise is rejected with the http response object.

services.User=function(){
    var self=this;
    self.save = function(user){
        var obj = {};
        obj._id=user._id;
        obj.fullname=user.fullname;
        obj.email=user.email;
        return obj;
    }
}

With a help of @georgeawg i came up to below:

service.User=function(){
    this.save = function(user,newUser){
        user._id=newUser._id;
        user.fullName=newUser.fullName;
        user.email=newUser.email;
    }
}

this is service which will host all functions is needed for shema User. Then we have those factories:

documents.MyUser=function(User){
    var user = angular.copy(User);
    user.obj={
        _id: "123",
        fullname: "Honchar Denys",
        email: "crackeraki@gmail.com",
    };
    return user;
}
documents.MyPartner=function(User){
    var user = angular.copy(User);
    user.obj={
        _id: "234",
        fullname: "Honchar Maria",
        email: "margood1990@gmail.com",
    };
    return user;
}

which return different User.obj.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM