vendredi 28 octobre 2016

Handling many states asynchronously in javascript

I am currently learning how to create bigger javascript projects and stumbled on an problem (Question below)

Here is a scenario:

var dialogOpen = 0;
var purchaseMode = 0;
var editMode = 0;
var currentlyEditing = 0;
var helper = 0;

I mean just an example of some flags that could be used for some purpose, all async of course. My question is, is there a better way to handle a crazy amount of states that need to be handled without having to spread state changes all over my application in an inconsistent manner?

if (editMode) { 
    editMode = 1;
   // Let the user edit all text
} else {
   editMode = 0;
}


if (target == purchaseButton) {
   purchaseMode = 1;
}

if (purchaseMode) {
   // allow purchases
}

I mean this is a small example, its harmless but I hope you understand the predicament im in. Imagine a software with 10.000 lines of code. Oh crap that would be pretty evil and extremely hard to maintain. What are some best practices to solve such a problem for larger scale programs?

Aucun commentaire:

Enregistrer un commentaire