/    Sign up×
Community /Pin to ProfileBookmark

Evaluating functions retrieved by ajax request in a DRY way?

Hello!

I have a MEN stack application that needs to retrieve functions from the server to run in the client. So far I have been passing in an array of functions, and parsing them. Everything is technically working but I’m having to repeat the same for loop in multiple functions.

APP.JS

[CODE]
app.get(“/logictest”, function(request, response){
response.send(myFuncs);

})

var myFunc = “function test1() {alert(‘test 1’);}”;
var myFunc2 = “function test2() {alert(‘test 2’);}”;
var myFuncs = [
myFunc,
myFunc2
];

[/CODE]

public js

[CODE]

// CALL OUR TEST FUNCTION FROM THE GAME LOGIC
var callTest1 = function(){
[COLOR=”#FF0000″] for (var i=0; i<theLogic.length;i++){
eval(theLogic[i]);
}[/COLOR]
test1();
}

// CALL OUR SECOND TEST FUNCTION FROM THE GAME LOGIC
var callTest2 = function(){
[COLOR=”#FF0000″] for (var i=0; i<theLogic.length;i++){
eval(theLogic[i]);
}[/COLOR]
test2();
}

$(document).ready(function() {
// AJAX REQUEST FOR GAME LOGIC
console.log(“requesting logic from server”);
$.ajax({
url: ‘https://thesite/logictest’,
type: ‘GET’,
success: function (data) {
console.log(‘success’, data);
// CREATE LOGIC ARRAY
theLogic =[];
// POPULATE THE LOGIC ARRAY WITH FUNCTIONS
for (var i = 0; i < data.length; i++){
console.log(‘Converting : ‘ + data[i]);
theLogic[i] = data[i];
var jsonVal = JSON.stringify(theLogic[i]);
theLogic[i] = $.parseJSON(jsonVal);
console.log(theLogic[i] + ” available to front end”);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
console.log(‘error’, errorThrown);
}
});

});

[/CODE]

Using

[CODE]
[B] for (var i=0; i<theLogic.length;i++){
eval(theLogic[i]);
}[/B]
[/CODE]

prior to calling the function is not only being used to make it available, but all of the other functions as well so that they can be accessed by the current function if needed.

This solution is technically working for me but there must be a way that I can evaluate everything in the theLogic array one time at the start and use that information to simply make test1() and test2() available everywhere on the page without having evaluate every item in the array every time I need to use a function.

Maybe I missed something, and there’s a better way to go about all of this in general. any advice would be appreciated!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@BillMurrayauthorJan 20.2016 — According to a gentleman in irc I should not ever use eval(), or transfer js with ajax. Instead I should leave the logic in the front end and obfuscate the code. So this problem doesn't need a solution (though i'm sure one exists).
×

Success!

Help @BillMurray spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...