0

I'm not sure what's the right question, but I'll try to explain in my current working code:

var container, loader
switch(section){
    case 'A':
        container = $('#list .container')
        loader = $('#surveylist')
        var r = postToServer(data, loader)
        if(r.cstatus === true){
            container.html(r.content)
        }
    break
    case 'B':
        container = $('#control .container')
        loader = container
        var r = postToServer(data, loader)
        if(r.cstatus === true){
            container.html(r.content.panelB)
        }
    break
    case 'C':
        container = $('#content .container')
        loader = container
        var r = postToServer(data, loader)
        if(r.cstatus === true){
            $('#surveyElements').sortable();
            $('textarea').autosize();
            container.html(r.content.panelC)
        }
    break
}

As you can see, I'm repeating the same code and would like to simplify the method, but not sure what the right term to search in Google. Basically what I'm trying to achieve is this structure:

var container, loader, test
switch(section){
    case 'A':,
        container = $('#list .container')
        loader = $('#surveylist')
        test =  container.html(r.content)
    break
    case 'B':
        container = $('#control .container')
        loader = container
        test =  container.html(r.content.panelB)

    break
    case 'C':
        container = $('#content .container')
        loader = container
        test = container.html(r.content.panelC)
    break
}
var r = postToServer(data, loader)
if(r.cstatus === true){
    // what is the right method to put 'test' here?
}

Or maybe if you guys have any suggestion on refactoring this code?

rolodex
  • 558
  • 1
  • 7
  • 19
  • "what is the right method to put 'test' here?" Put here, what do you mean??? Anyway, checking your code, `test` is equal to `container`. I really don't understand what you are looking for – A. Wolff Mar 25 '14 at 10:09
  • The variable `test` is assigned within each `case`. But as you can see, variable `r` is only declared after the `switch`, so each `test` inside `case` is invalid. Maybe I should make `test` into a function instead of variable? If so, how do I call acknowledge `r` inside `test`? – rolodex Mar 25 '14 at 10:11
  • So then set logic using `r` inside request callback or use promise interface methods. Search for samples on how to use ajax http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – A. Wolff Mar 25 '14 at 10:12
  • Ok. But I'm having trouble understanding the concept. Thanks for the link though. I'll be fathoming now. – rolodex Mar 25 '14 at 10:18

1 Answers1

1

It's just a draft and still can be optimized but might help.

var sections = {
    A: {
        container: $('#list .container'),
        loader: $('#surveylist'),
        getTest: (function(container, r){ return container.html(r.content) })
    },
    B: {
        container: $('#control .container'),
        loader: $('#control .container'),
        getTest: (function(container, r){ return container.html(r.content.panelB) })
    },
    C: {
        container: $('#content .container'),
        loader: $('#content .container'),
        getTest: (function(container, r){ return container.html(r.content.panelC) })
    },
};
var sectionEntry = sections[section];
r = postToServer(data, sectionEntry.loader);
if(r.cstatus === true){
    test = sectionEntry.getTest(sectionEntry.container, r);
}
ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • This is a new insight for me. I've never thought of doing it the objective way, I always resort to `switch`! Thanks man. This is getting me closer to backbone.js! – rolodex Mar 25 '14 at 10:27