Quantcast
Channel: DreamFactory Forum - Latest posts
Viewing all articles
Browse latest Browse all 5027

Only GET requests work after update to 2.1

$
0
0

I found that I could use the following function and reference it in the create() for each factory resource.

function wrapResource(data){        
    switch(typeof(data)){
        case "string":
            return '{"resource": '+data+'}';
            break;
        case "object":
            var d = angular.isArray(data) ? data : [data];
            angular.forEach(d,function(value, key){
                if (value.id === null || value.id === ""){
                    //console.log("found null id");
                    delete value.id;
                }
            });
            return '{"resource": '+angular.toJson(d)+"}";
            break;
    }
    return data;
}

Then in the factory, include this function as the transformRequest:

// Contacts factory to fetch contacts data from dreamfactory services.
.factory('Contacts', [
    '$resource',

    function ($resource) {
        return $resource('/api/v2/db/_table/contact/:id', { id: '@id' }, {
            query: {
                method: 'GET',
                isArray: false
            },
            create: {
                method: 'POST',
                transformRequest: wrapResource       // <<<<<<<<< added just this line
            },
            update: {
                method: 'PUT'
            },
            remove: {
                method: 'DELETE'
            }
        });
    }
])

Viewing all articles
Browse latest Browse all 5027

Trending Articles