Skip to content

Instantly share code, notes, and snippets.

@markmarijnissen
Created July 5, 2017 18:06
Show Gist options
  • Save markmarijnissen/70e4bd1ba9d776c0f73ce9b67b681c45 to your computer and use it in GitHub Desktop.
Save markmarijnissen/70e4bd1ba9d776c0f73ce9b67b681c45 to your computer and use it in GitHub Desktop.
Placeholder function
// 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
}
}
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