Pregunta

I'm a beginner with angularjs and mongolab ..

I have this code, to edit a record in mongolab :

function EditCtrl($scope, $location, $routeParams, Project) {
    var self = this;
    Project.get({id: $routeParams.projetId}, function(projet) {
        self.original = projet;
        $scope.projet = new Project(self.original);
    });

    $scope.save = function() {
        $scope.projet.update(function() {
            $location.path('/list');
        });
    };
}

It works perfectly. I wanted to display all the keys and values from the record, this is the code :

<div ng-repeat="(key, val) in projet">
    <div>{{key}}:{{val}}</div>
</div>

And this is the result :

_id:{} destroy: name:Test test rang:4 update:

In my record, i only have the _id, name and rang. I don't know why "destroy:" dans "update:" are displayed! probably because i use this code to connect to mongolab :

angular.module('mongolab', ['ngResource']).
        factory('Project', function($resource) {
    var Project = $resource('https://api.mongolab.com/api/1/databases' +
            '/_____/collections/_________/:id',
            {apiKey: '___________________'}, {
        update: {method: 'PUT'}
    }
    );

    Project.prototype.update = function(cb) {
        return Project.update({id: this._id.$oid},
        angular.extend({}, this, {_id: undefined}), cb);
    };

    Project.prototype.destroy = function(cb) {
        return Project.remove({id: this._id.$oid}, cb);
    };

    return Project;
});

What should i do to only display the record data ?

thanks

¿Fue útil?

Solución

Return a service and pass the item you receive from get() back to update and destroy.

factory('Project', function($resource) {
return { 
     get: function() {
         return $resource('https://api.mongolab.com/api/1/databases' +
        '/_____/collections/_________/:id',
        {apiKey: '___________________'}, {
         update: {method: 'PUT'}
      },


update : function(itm, cb) {
    return item.update({id: item._id.$oid},
    angular.extend({}, item, {_id: undefined}), cb);
},

destroy : function(item, cb) {
    return item.remove({id: item._id.$oid}, cb);
};

Otherwise you can instantiate only one and reference it

factory('Project', function($resource) {
var item =$resource('https://api.mongolab.com/api/1/databases' +
        '/_____/collections/_________/:id',
        {apiKey: '___________________'}, {
         update: {method: 'PUT'}
return { 


update : function(cb) {
    return item.update({id: item._id.$oid},
    angular.extend({}, item, {_id: undefined}), cb);
},

destroy : function(cb) {
    return item.remove({id: item._id.$oid}, cb);
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top