Created
July 5, 2017 18:06
-
-
Save markmarijnissen/70e4bd1ba9d776c0f73ce9b67b681c45 to your computer and use it in GitHub Desktop.
Placeholder function
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
// In your parent | |
import placeholderfunction from "./placeholderfunction"; | |
class Parent extends Component { | |
// A placeholder function is a function that is not yet implemented | |
save = placeholderfunction(); | |
render() { | |
<button onClick={this.save}>Save</button> | |
<Child save={this.save} /> | |
} | |
} | |
// In your child controller | |
class ChildCtrl { | |
constructor(props) { | |
// Implement the Placeholder function with "this.save" | |
props.save.implement(this.save); | |
} | |
save() { | |
// do the work | |
} | |
} |
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
export default function createPlaceholderfunction() { | |
var context = this; | |
var implementation; | |
var calls = []; | |
const func = function placeholderfunction() { | |
if(typeof implementation === "function") { | |
implementation.apply(context,arguments); | |
} else { | |
calls.push(arguments); | |
} | |
}; | |
func.implement = (_implementation, _context) => { | |
implementation = _implementation; | |
if(_context) context = _context; | |
calls.forEach(args => implementation.apply(context,args)); | |
}; | |
return func; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment