0

Just a quick question how to avoid duplication of code when doing a http post and put of the same form.

say:

// Post new form
function postForm(form){
    var data = {
        name: form.name
        lastName: form.lastName
    }

    return api.post('jobs', undefined, JSON.stringify(data), 'application/json');
}

// Put save/edit existing (data)
function putForm(form){
    var data = {
        name: form.name
        lastName: form.lastName
    }

    return api.put('jobs', undefined, JSON.stringify(data), 'application/json');
}

My attempt, but then I don't have access to function form parameter.

var data = {
    name: name
    lastName: lastName
}

// Post new form
function postForm(form){
    return api.post('jobs', undefined, JSON.stringify(data), 'application/json');
}

// Put save edit form
function postForm(form){
    return api.put('jobs', undefined, JSON.stringify(data), 'application/json');
}

Not sure if I'm making sense here but hopefully someone will get what I'm trying to do. Basically I don't want to duplicate the var data when calling api post or put.

3 Answers 3

2

Maybe use a normalizing helper function?

// Post new form
function postForm(form){
    return $http.post('api', extractData(form));
}

// Put save/edit existing (data)
function putForm(form){
    return $http.put('api', extractData(form));
}


// private helper    
function extractData(data){
    return JSON.stringify({
        name: data.name,
        lastName: data.lastName
    });
}

Of course you can add some configuration to the normalizer if you need subtle changes per implementation.

Sign up to request clarification or add additional context in comments.

2 Comments

See my update above, atm that's how I'm sending the objs. Seems to work fine, I wrapped the extractData in JSON.stringify because it was giving me a 400 err. But which is better to use this approach or using JSON.parse(JSON.stringify(form)). Objs are coming from the back-end
Not sure why you would stringify and then parse, but if you need the JSON.parse result, just do that in your helper function. See updated example.
0

You have two function implementations, and the only thing that's different between the two is the http function used.

In javascript, functions are first class objects, so you can pass the http function as a parameter..

function send(form, httpMethod) {
   var data = {
     name: form.name
     lastName: form.lastName
   };
   return httpMethod.apply($http, [
     'jobs',
     undefined,
     JSON.stringify(data),
     'application/json'
   ]);
 }

 // Post new form
 function postForm(form) {
   send(form, $http.post);
 }

 // Put save edit form
 function putForm(form) {
   send(form, $http.put);
 }

It is necessary to invoke the method through apply, rather than simply invoking httpMethod('api', data);, because the methods may rely on fields hanging off the this pointer.

Comments

0

Haven't tested it, but this might work for you

function postForm(form, method){

    var data = {
        name: form.name
        lastName: form.lastName
    };

    return $http({
                  url: 'api', 
                  method: method,
                  data: data
                  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.