|
|
|
|
|
by joshstrange
4120 days ago
|
|
I use something a little similar where I work. No one would be on board with something like React but I've documented a way to write our code that does something similar to this post. We use js "classes" (Both Function.prototype and now js classes with babel) and lo-dash templates to make it a little nicer. Example: function MyComponent(element) {
var instance = this;
instance.element = element;
instance.history = [];
instance.state = { myKey: 'someData' };
instance.template = _.template($('my-template').html());
instance.setupHandlers();
}
MyComponent.prototype.render = function() {
var instance = this;
instance.element.html(instance.template(instance.state));
instance.history.push(deepcopy(instance.state));
}
MyComponent.prototype.undo = function() {
var instance = this;
instance.state = instance.history.pop();
instance.render();
}
MyComponent.prototype.setupHandlers = function() {
instance.element.on('change', '.my-input', function(e) {
instance.state.myInput = $(this).val();
instance.render();
});
}
This is of course a very simplified version but it should give you an idea of what I'm talking about. |
|