Last active
September 10, 2018 22:28
-
-
Save austin-sa-wang/a9cdea533d6a9f959199448f1c385d65 to your computer and use it in GitHub Desktop.
Compare different organization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Background: | |
We have a component with two buttons, one button for adding a new empty fee variation, another for duplicating an existing fee variation. | |
for example: | |
button(click='uiModel.addVariation()') Add New | |
button(click='uiModel.duplicateVariation(seed)') Duplicate | |
There are four different implementations: | |
1. v1 original | |
2. v2a complete separation | |
3. v2a-alt complete separation with extraction | |
4. v2b merged | |
*/ | |
// v1 original | |
const uiModel = { | |
addVariation(duplicatedVariationModel = null) { | |
var newEntry = duplicatedVariationModel || new FeeVariationModel({ | |
context: this | |
}) | |
this.variations.push(newEntry) | |
this.formValidator.setDirty() | |
}, | |
duplicateVariation(variationModel) { | |
const duplicatedVariationModel = new FeeVariationModel({ | |
data: variationModel.clone(), | |
context: this | |
}) | |
this.addVariation(duplicatedVariationModel) | |
this.formValidator.setDirty() | |
} | |
} | |
// v2a complete separation | |
const uiModel = { | |
addVariation() { | |
var newEntry = new FeeVariationModel({ | |
context: this | |
}) | |
this.variations.push(newEntry) | |
this.formValidator.setDirty() | |
}, | |
duplicateVariation(variationModel) { | |
const duplicatedVariationModel = new FeeVariationModel({ | |
data: variationModel.clone(), | |
context: this | |
}) | |
this.variations.push(duplicatedVariationModel) | |
this.formValidator.setDirty() | |
} | |
} | |
// v2a-alt complete separation with extraction | |
const uiModel = { | |
add(model) { // this feels awkward, so have logic specific function to be on the same domain as the rest of ui action functions. | |
this.variations.push(newEntry) | |
this.formValidator.setDirty() | |
}, | |
createVariation() { | |
var newEntry = new FeeVariationModel({ | |
context: this | |
}) | |
this.add(newEntry) | |
}, | |
duplicateVariation(variationModel) { | |
const duplicatedVariationModel = new FeeVariationModel({ | |
data: variationModel.clone(), | |
context: this | |
}) | |
this.add(duplicatedVariationModel) | |
} | |
} | |
// v2b merged | |
const uiModel = { | |
addVariation(seed = null) { | |
const newEntry = new FeeVariationModel({ | |
data: isDefined(seed) ? seed.clone() : null, | |
context: this | |
}) | |
this.variations.push(newEntry) | |
this.formValidator.setDirty() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment