damn awesome local storage now

This commit is contained in:
Peter Cottle 2012-10-23 10:39:06 -07:00
parent 0fbbbcf35d
commit 7e28fd8452
4 changed files with 63 additions and 8 deletions

View file

@ -10,6 +10,11 @@ var BranchCollection = Backbone.Collection.extend({
model: Branch model: Branch
}); });
var CommandEntryCollection = Backbone.Collection.extend({
model: CommandEntry,
localStorage: new Backbone.LocalStorage('CommandEntries')
});
var CommandBuffer = Backbone.Model.extend({ var CommandBuffer = Backbone.Model.extend({
defaults: { defaults: {
collection: null, collection: null,

View file

@ -305,3 +305,11 @@ OptionParser.prototype.explodeAndSet = function() {
// done! // done!
}; };
// command entry is for the commandview
var CommandEntry = Backbone.Model.extend({
defaults: {
text: ''
},
localStorage: new Backbone.LocalStorage('CommandEntries')
});

View file

@ -1,7 +1,26 @@
var CommandPromptView = Backbone.View.extend({ var CommandPromptView = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
this.collection = options.collection; this.collection = options.collection;
this.commands = [];
// uses local storage
this.commands = new CommandEntryCollection();
this.commands.fetch({
success: _.bind(function() {
// reverse the commands. this is ugly but needs to be done...
var commands = [];
this.commands.each(function(c) {
commands.push(c);
});
commands.reverse();
this.commands.reset();
_.each(commands, function(c) {
this.commands.add(c);
}, this);
}, this)
});
this.index = -1; this.index = -1;
this.commandSpan = this.$('#prompt span.command')[0]; this.commandSpan = this.$('#prompt span.command')[0];
@ -134,7 +153,7 @@ var CommandPromptView = Backbone.View.extend({
this.index += delta; this.index += delta;
// if we are over / under, display blank line. yes this eliminates your // if we are over / under, display blank line. yes this eliminates your
// partially written command, but i doubt that is much in this demo // partially edited command, but i doubt that is much in this demo
if (this.index >= this.commands.length || this.index < 0) { if (this.index >= this.commands.length || this.index < 0) {
this.clear(); this.clear();
this.index = -1; this.index = -1;
@ -142,7 +161,15 @@ var CommandPromptView = Backbone.View.extend({
} }
// yay! we actually can display something // yay! we actually can display something
this.setTextField(this.commands[this.index]); var commandEntry = this.commands.toArray()[this.index].get('text');
this.setTextField(commandEntry);
},
clearLocalStorage: function() {
this.commands.each(function(c) {
Backbone.sync('delete', c, function() { });
}, this);
localStorage.setItem('CommandEntries', '');
}, },
setTextField: function(value) { setTextField: function(value) {
@ -160,9 +187,23 @@ var CommandPromptView = Backbone.View.extend({
}, },
submitValue: function(value) { submitValue: function(value) {
// if we are entering a real command, add it to our history // we should add if it's not a blank line and this is a new command...
if (value.length) { // or if we edited the command
this.commands.unshift(value); var shouldAdd = (value.length && this.index == -1) ||
((value.length && this.index !== -1 &&
this.commands.toArray()[this.index].get('text') !== value));
if (shouldAdd) {
var commandEntry = new CommandEntry({text: value});
this.commands.unshift(commandEntry);
// store to local storage
Backbone.sync('create', commandEntry, function() { });
// if our length is too egregious, reset
if (this.commands.length > 100) {
this.clearLocalStorage();
}
} }
this.index = -1; this.index = -1;

View file

@ -24,11 +24,12 @@ $(document).ready(function(){
collection: commandCollection collection: commandCollection
}); });
new CommandPromptView({ // TODO make not global
commandPromptView = new CommandPromptView({
el: $('#commandLineBar'), el: $('#commandLineBar'),
collection: commandCollection collection: commandCollection
}); });
new CommandLineHistoryView({ commandLineHistoryView = new CommandLineHistoryView({
el: $('#commandLineHistory'), el: $('#commandLineHistory'),
collection: commandCollection collection: commandCollection
}); });