Created
April 2, 2015 20:25
-
-
Save lamchau/eef5668356190071fc5c to your computer and use it in GitHub Desktop.
ember.js: command pattern with browser history for undo/redo
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
| /** | |
| * Command Dispatcher for managing {@code App.Command}. | |
| * | |
| * {@link http://en.wikipedia.org/wiki/Command_pattern|Command Pattern} | |
| */ | |
| App.CommandController = Ember.Controller.extend({ | |
| commands: null, | |
| result: null, | |
| position: 0, | |
| maxRedo: 0, | |
| _init: function() { | |
| // we must reset complex types to avoid call by object references (i.e. call | |
| // by sharing) for each instance | |
| this.setProperties({ | |
| commands: [], | |
| result: [] | |
| }); | |
| }.on("init"), | |
| execute: function(command) { | |
| Ember.assert("Argument must be an instance of App.Command", | |
| App.Command.detectInstance(command)); | |
| this.commands[this.incrementProperty("position")] = command; | |
| this.setProperties({ | |
| maxRedo: 0, | |
| result: command.execute(this.getResult()), | |
| }); | |
| }, | |
| getResult: function() { | |
| return this.get("result"); | |
| }, | |
| redo: function(count) { | |
| var command, | |
| position = this.get("position") + 1, | |
| maxRedo = this.get("maxRedo"); | |
| count = Math.min(arguments.length ? Math.abs(count) : 1, maxRedo); | |
| for (var i = 0; i < count; i++) { | |
| command = this.commands[position + i]; | |
| this.set("result", command.execute(this.getResult())); | |
| this.decrementProperty("maxRedo"); | |
| } | |
| this.incrementProperty("position", count); | |
| }, | |
| reset: function() { | |
| var position = this.get("position"); | |
| this.setProperties({ | |
| commands: [], | |
| maxRedo: 0, | |
| position: 0, | |
| result: [], | |
| }); | |
| if (position > 0) { | |
| Ember.History.go(-position); | |
| } | |
| }, | |
| undo: function(count) { | |
| var command, | |
| position = this.get("position"); | |
| count = Math.min(arguments.length ? Math.abs(count) : 1, position); | |
| for (var i = 0; i < count; i++) { | |
| command = this.commands.get(position); | |
| this.set("result", command.undo(this.getResult())); | |
| this.incrementProperty("maxRedo"); | |
| this.decrementProperty("position"); | |
| } | |
| } | |
| }); |
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
| App.MyController = Ember.Controller.extend({ | |
| breadcrumbs: [], | |
| actions: { | |
| navigate: function(index) { | |
| // navigation undo is current depth - clicked depth | |
| var breadcrumbs = this.get("breadcrumbs"), | |
| current = _.last(breadcrumbs), | |
| selected = breadcrumbs[index], | |
| count = current.depth - selected.depth; | |
| if (count > 0) { | |
| Ember.History.go(-count); | |
| } | |
| } | |
| }, | |
| renderBreadcrumb: function(node, direction) { | |
| var nodes = [], | |
| parent = node; | |
| while (parent) { | |
| nodes.unshift(parent); | |
| parent = parent.parent; | |
| } | |
| if (direction === "up" && nodes) { | |
| nodes.pop(); | |
| } | |
| this.set("breadcrumbs", nodes); | |
| } | |
| }); |
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
| (function(History) { | |
| if (!Ember.Object.detectInstance(History)) { | |
| throw new Error("Native `Ember.History` is already defined."); | |
| } | |
| if (Ember.Evented.detect(History)) { | |
| return; | |
| } else { | |
| History.reopen(Ember.Evented); | |
| } | |
| History.reopen({ | |
| back: function() { | |
| window.history.go(-1); | |
| }, | |
| forward: function() { | |
| window.history.go(1); | |
| }, | |
| go: function(count) { | |
| // most likely an error, explicitly use reload when we mean it | |
| if (count === 0) { | |
| throw new Ember.Error("Use `window.location.reload()` instead"); | |
| } | |
| Ember.assert("Count must be an integer", Math.round(count) === count); | |
| window.history.go(count); | |
| this.trigger("go", count); | |
| }, | |
| pushState: function(state, title, url) { | |
| window.history.pushState(state, title, url); | |
| this.trigger("pushState", state, title, url); | |
| }, | |
| replaceState: function(state, title, url) { | |
| window.history.replaceState(state, title, url); | |
| this.trigger("replaceState", state, title, url); | |
| } | |
| }); | |
| })(Ember.History || (Ember.History = Ember.Object.create())); |
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
| App.Command = Ember.Object.extend({ | |
| execute: function() { | |
| Ember.assert("Method 'execute' not implemented"); | |
| }, | |
| undo: function() { | |
| // optional | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment