From: Zachary Vance Date: Wed, 20 May 2015 00:24:16 +0000 (-0700) Subject: Use getters and setters to make code cleaner X-Git-Url: https://git.za3k.com/?a=commitdiff_plain;h=119b8700fe214cf8fb586bc84bc829bae80a9811;p=flowy.git Use getters and setters to make code cleaner --- diff --git a/dist/flowy.js b/dist/flowy.js index 83ad047..9d8e6d2 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -72,7 +72,7 @@ var TodoView = Backbone.View.extend({ //"click > .checkbox": "toggleComplete", "input > .text": "textChange", "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end - "keydown > .text": "keydown", + //"keydown > .text": "keydown", }, initialize: function() { this.childViewPositions = []; @@ -377,8 +377,8 @@ var FlowyDocModel = Backbone.Collection.extend({ comparator: 'id', }); -// TODO: None of this code has ever been run and doesn't work // TODO: More documentation +// TODO: Removing objects/shortcuts/rebinding won't work until this bug is fixed in mousetrap: https://github.com/ccampbell/mousetrap/issues/267 // firstShortcut = Shortcut.registerShortcut("Control + k", function() { console.log("Shortcut pressed."); }); // firstShortcut.rebind("Control + m"); @@ -390,7 +390,7 @@ var Shortcut = (function(document, _) { var globalMousetrap = new Mousetrap(); var globalObject = {element: document, type: "global", isGlobal: true, mousetrap: globalMousetrap}; var Shortcut = { - globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked. + _globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked. globalMousetrap: globalMousetrap, boundObjects: [globalObject], shortcuts: [], @@ -411,21 +411,28 @@ var Shortcut = (function(document, _) { unbind: function(object) { manager.unbindShortcut(this, object); }, rebind: function(keybinding) { manager.rebindShortcut(this, keybinding); }, object: "global", - keybinding: "", + + _keybinding: shortcut.keybinding || "", action: function() {}, boundObjects: [], ownedByUs: true }); + Object.defineProperty(shortcut, "keybinding", { + set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); }, + get: function () { return shortcut._keybinding; }, + }); this.shortcuts.push(shortcut); _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value(); return shortcut; }, unregisterShortcut: function(shortcut) { - _.each(shortcut.boundObjects, _.partial(this.unbindShortcut, shortcut, _), this); // TODO: This will die under mutation, find a safer 'each' method + _.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this); }, bindShortcut: function(shortcut, object) { // Do not call directly - object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object)); - shortcut.boundObjects.push(object); + if (!_.contains(shortcut.boundObjects, object)) { + object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object)); + shortcut.boundObjects.push(object); + } }, unbindShortcut: function(shortcut, object) { // Do not call directly with an object if (!object){ @@ -435,12 +442,11 @@ var Shortcut = (function(document, _) { shortcut.boundObjects = _.without(shortcut.boundObjects, object); }, rebindShortcut: function(shortcut, keybinding) { - // TODO: figure out a way to do this with 'shorcut.keybinding=' binding if (keybinding === shortcut.keybinding) return; var oldKeybinding = shortcut.keybinding; var objects = shortcut.boundObjects; shortcut.unbind(); - shorcut.keybinding = keybinding; + shorcut._keybinding = keybinding; _.each(objects, shortcut.bind, shortcut); if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding); }, @@ -469,7 +475,7 @@ var Shortcut = (function(document, _) { }); }, shortcutPressed: function(shortcut, object) { - if (shortcut.action) shortcut.action(object, object, shortcut); + if (shortcut.action) shortcut.action(object.element, shortcut, object.type); }, onRebindShortcut: function(shortcut, keybinding, oldKeybinding) { // NOTE: You may want to hook into this if you want to save/load user preferences @@ -481,7 +487,8 @@ var Shortcut = (function(document, _) { element:element, type:type, mousetrap: new Mousetrap(element), - remove: function() { manager.removeObject(this); } + remove: function() { manager.removeObject(this); }, + add: function() { manager.addObject(this.element, this.type); }, }; _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value(); if (!_.contains(this.boundObjects)) this.boundObjects.push(object); @@ -491,17 +498,19 @@ var Shortcut = (function(document, _) { _.each(this.shortcuts, _.partial(this.unbindShortcut, _, object), this); this.boundObjects = _.without(this.boundObjects, object); }, - changeGlobalObject: function(newObject) { - // TODO: Figure out how to do this with 'this.globalObject=' binding + get globalObject() { + return this._globalObject; + }, + set globalObject(newElement) { this.removeObject(this.globalObject, "global"); - this.globalObject = { - element: element, - type: type, + this._globalObject = { + element: newElement, + type: "global", isGlobal: true, mousetrap: this.globalMousetrap, }; - _.chain(this.shortcuts).where({'object': type}).each(function(shortcut) { this.bindShortcut(shortcut, object); }, this).value(); - if (!_.contains(this.boundObjects)) this.boundObjects.push(object); + _.chain(this.shortcuts).where({'object': "global"}).each(_.partial(this.bindShortcut, _, this.globalObject), this).value(); + if (!_.contains(this.boundObjects)) this.boundObjects.push(this.globalObject); return this.globalObject; }, pause: function() { // TODO: These require plugin, test diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index 1c19230..ccb133b 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -71,7 +71,7 @@ var TodoView = Backbone.View.extend({ //"click > .checkbox": "toggleComplete", "input > .text": "textChange", "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end - "keydown > .text": "keydown", + //"keydown > .text": "keydown", }, initialize: function() { this.childViewPositions = []; @@ -376,8 +376,8 @@ var FlowyDocModel = Backbone.Collection.extend({ comparator: 'id', }); -// TODO: None of this code has ever been run and doesn't work // TODO: More documentation +// TODO: Removing objects/shortcuts/rebinding won't work until this bug is fixed in mousetrap: https://github.com/ccampbell/mousetrap/issues/267 // firstShortcut = Shortcut.registerShortcut("Control + k", function() { console.log("Shortcut pressed."); }); // firstShortcut.rebind("Control + m"); @@ -389,7 +389,7 @@ var Shortcut = (function(document, _) { var globalMousetrap = new Mousetrap(); var globalObject = {element: document, type: "global", isGlobal: true, mousetrap: globalMousetrap}; var Shortcut = { - globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked. + _globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked. globalMousetrap: globalMousetrap, boundObjects: [globalObject], shortcuts: [], @@ -410,21 +410,28 @@ var Shortcut = (function(document, _) { unbind: function(object) { manager.unbindShortcut(this, object); }, rebind: function(keybinding) { manager.rebindShortcut(this, keybinding); }, object: "global", - keybinding: "", + + _keybinding: shortcut.keybinding || "", action: function() {}, boundObjects: [], ownedByUs: true }); + Object.defineProperty(shortcut, "keybinding", { + set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); }, + get: function () { return shortcut._keybinding; }, + }); this.shortcuts.push(shortcut); _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value(); return shortcut; }, unregisterShortcut: function(shortcut) { - _.each(shortcut.boundObjects, _.partial(this.unbindShortcut, shortcut, _), this); // TODO: This will die under mutation, find a safer 'each' method + _.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this); }, bindShortcut: function(shortcut, object) { // Do not call directly - object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object)); - shortcut.boundObjects.push(object); + if (!_.contains(shortcut.boundObjects, object)) { + object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object)); + shortcut.boundObjects.push(object); + } }, unbindShortcut: function(shortcut, object) { // Do not call directly with an object if (!object){ @@ -434,12 +441,11 @@ var Shortcut = (function(document, _) { shortcut.boundObjects = _.without(shortcut.boundObjects, object); }, rebindShortcut: function(shortcut, keybinding) { - // TODO: figure out a way to do this with 'shorcut.keybinding=' binding if (keybinding === shortcut.keybinding) return; var oldKeybinding = shortcut.keybinding; var objects = shortcut.boundObjects; shortcut.unbind(); - shorcut.keybinding = keybinding; + shorcut._keybinding = keybinding; _.each(objects, shortcut.bind, shortcut); if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding); }, @@ -468,7 +474,7 @@ var Shortcut = (function(document, _) { }); }, shortcutPressed: function(shortcut, object) { - if (shortcut.action) shortcut.action(object, object, shortcut); + if (shortcut.action) shortcut.action(object.element, shortcut, object.type); }, onRebindShortcut: function(shortcut, keybinding, oldKeybinding) { // NOTE: You may want to hook into this if you want to save/load user preferences @@ -480,7 +486,8 @@ var Shortcut = (function(document, _) { element:element, type:type, mousetrap: new Mousetrap(element), - remove: function() { manager.removeObject(this); } + remove: function() { manager.removeObject(this); }, + add: function() { manager.addObject(this.element, this.type); }, }; _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value(); if (!_.contains(this.boundObjects)) this.boundObjects.push(object); @@ -490,17 +497,19 @@ var Shortcut = (function(document, _) { _.each(this.shortcuts, _.partial(this.unbindShortcut, _, object), this); this.boundObjects = _.without(this.boundObjects, object); }, - changeGlobalObject: function(newObject) { - // TODO: Figure out how to do this with 'this.globalObject=' binding + get globalObject() { + return this._globalObject; + }, + set globalObject(newElement) { this.removeObject(this.globalObject, "global"); - this.globalObject = { - element: element, - type: type, + this._globalObject = { + element: newElement, + type: "global", isGlobal: true, mousetrap: this.globalMousetrap, }; - _.chain(this.shortcuts).where({'object': type}).each(function(shortcut) { this.bindShortcut(shortcut, object); }, this).value(); - if (!_.contains(this.boundObjects)) this.boundObjects.push(object); + _.chain(this.shortcuts).where({'object': "global"}).each(_.partial(this.bindShortcut, _, this.globalObject), this).value(); + if (!_.contains(this.boundObjects)) this.boundObjects.push(this.globalObject); return this.globalObject; }, pause: function() { // TODO: These require plugin, test diff --git a/flowy.tmux.conf b/flowy.tmux.conf index 68d57ef..d3424c4 100755 --- a/flowy.tmux.conf +++ b/flowy.tmux.conf @@ -5,7 +5,7 @@ new-window -n "source" -c ~/flowy/src "vim -p *.html */*" new-window -n timelog "vim ~/documents/paul-timelog" # 'b' runs a browser -bind-key b run-shell -b "chromium file:///home/zachary/flowy/dist/main.html" +bind-key b run-shell -b "chromium file:///home/zachary/flowy/dist/index.html" # 'd' runs a browser and opens documentation bind-key d run-shell -b "chromium http://backbonejs.org/" # 'r' reloads this file (doesn't work because of the sessions and windows at the top) diff --git a/src/library/shortcut.js b/src/library/shortcut.js index 6bd0860..75f46ee 100644 --- a/src/library/shortcut.js +++ b/src/library/shortcut.js @@ -1,5 +1,5 @@ -// TODO: None of this code has ever been run and doesn't work // TODO: More documentation +// TODO: Removing objects/shortcuts/rebinding won't work until this bug is fixed in mousetrap: https://github.com/ccampbell/mousetrap/issues/267 // firstShortcut = Shortcut.registerShortcut("Control + k", function() { console.log("Shortcut pressed."); }); // firstShortcut.rebind("Control + m"); @@ -11,7 +11,7 @@ var Shortcut = (function(document, _) { var globalMousetrap = new Mousetrap(); var globalObject = {element: document, type: "global", isGlobal: true, mousetrap: globalMousetrap}; var Shortcut = { - globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked. + _globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked. globalMousetrap: globalMousetrap, boundObjects: [globalObject], shortcuts: [], @@ -32,21 +32,28 @@ var Shortcut = (function(document, _) { unbind: function(object) { manager.unbindShortcut(this, object); }, rebind: function(keybinding) { manager.rebindShortcut(this, keybinding); }, object: "global", - keybinding: "", + + _keybinding: shortcut.keybinding || "", action: function() {}, boundObjects: [], ownedByUs: true }); + Object.defineProperty(shortcut, "keybinding", { + set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); }, + get: function () { return shortcut._keybinding; }, + }); this.shortcuts.push(shortcut); _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value(); return shortcut; }, unregisterShortcut: function(shortcut) { - _.each(shortcut.boundObjects, _.partial(this.unbindShortcut, shortcut, _), this); // TODO: This will die under mutation, find a safer 'each' method + _.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this); }, bindShortcut: function(shortcut, object) { // Do not call directly - object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object)); - shortcut.boundObjects.push(object); + if (!_.contains(shortcut.boundObjects, object)) { + object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object)); + shortcut.boundObjects.push(object); + } }, unbindShortcut: function(shortcut, object) { // Do not call directly with an object if (!object){ @@ -56,12 +63,11 @@ var Shortcut = (function(document, _) { shortcut.boundObjects = _.without(shortcut.boundObjects, object); }, rebindShortcut: function(shortcut, keybinding) { - // TODO: figure out a way to do this with 'shorcut.keybinding=' binding if (keybinding === shortcut.keybinding) return; var oldKeybinding = shortcut.keybinding; var objects = shortcut.boundObjects; shortcut.unbind(); - shorcut.keybinding = keybinding; + shorcut._keybinding = keybinding; _.each(objects, shortcut.bind, shortcut); if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding); }, @@ -90,7 +96,7 @@ var Shortcut = (function(document, _) { }); }, shortcutPressed: function(shortcut, object) { - if (shortcut.action) shortcut.action(object, object, shortcut); + if (shortcut.action) shortcut.action(object.element, shortcut, object.type); }, onRebindShortcut: function(shortcut, keybinding, oldKeybinding) { // NOTE: You may want to hook into this if you want to save/load user preferences @@ -102,7 +108,8 @@ var Shortcut = (function(document, _) { element:element, type:type, mousetrap: new Mousetrap(element), - remove: function() { manager.removeObject(this); } + remove: function() { manager.removeObject(this); }, + add: function() { manager.addObject(this.element, this.type); }, }; _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value(); if (!_.contains(this.boundObjects)) this.boundObjects.push(object); @@ -112,17 +119,19 @@ var Shortcut = (function(document, _) { _.each(this.shortcuts, _.partial(this.unbindShortcut, _, object), this); this.boundObjects = _.without(this.boundObjects, object); }, - changeGlobalObject: function(newObject) { - // TODO: Figure out how to do this with 'this.globalObject=' binding + get globalObject() { + return this._globalObject; + }, + set globalObject(newElement) { this.removeObject(this.globalObject, "global"); - this.globalObject = { - element: element, - type: type, + this._globalObject = { + element: newElement, + type: "global", isGlobal: true, mousetrap: this.globalMousetrap, }; - _.chain(this.shortcuts).where({'object': type}).each(function(shortcut) { this.bindShortcut(shortcut, object); }, this).value(); - if (!_.contains(this.boundObjects)) this.boundObjects.push(object); + _.chain(this.shortcuts).where({'object': "global"}).each(_.partial(this.bindShortcut, _, this.globalObject), this).value(); + if (!_.contains(this.boundObjects)) this.boundObjects.push(this.globalObject); return this.globalObject; }, pause: function() { // TODO: These require plugin, test diff --git a/src/views/todo.js b/src/views/todo.js index 81cbb5c..e1a2d3a 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -10,7 +10,7 @@ var TodoView = Backbone.View.extend({ //"click > .checkbox": "toggleComplete", "input > .text": "textChange", "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end - "keydown > .text": "keydown", + //"keydown > .text": "keydown", }, initialize: function() { this.childViewPositions = [];