Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Last active December 20, 2015 11:09
Show Gist options
  • Save kristoferjoseph/6120902 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/6120902 to your computer and use it in GitHub Desktop.
Backbone Memento Example
createMemento: function (hash) {
var memento = new Memento({
originator: this,
state: this.changedAttributes(hash)
});
return memento;
},
//Sets a memento object to restore state.
// Override this function to supply your own way to restore state.
setMemento: function (memento) {
var state = memento.state;
if (this.set) {
this.set(state);
}
return this;
},
//Creates a memento and sends it along with an event to be stored then calls set
store: function (hash) {
//Checks to see if there is anything in the hash before doing any work
if ( !_.isEmpty(hash) ) {
this.eventMap.trigger("Memento:Store", this.createMemento(hash));
//Calls set after storing the delta
this.set(hash);
}
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment