ruk·si

🦴 Backbone
Model Cache

Updated at 2013-11-16 12:18

Simple cache implementation for Backbone Models.

lookup: function(id) {
    var model = this.get(id);
    if (model) {
        return $.Deferred().resolveWith(this, model);
    }
    else {
        model = new Model({id: id});
        return model.fetch();
    }
}

users.lookup(id).then(function(){});

Storing the previous states e.g. for undo functionality.

this.on('change', function() {
    changes.push(this.previousAttributes());
});
undo: function() {
    undone.push(this.toJSON());
    this.set(changes.pop());
}
redo: function() {
    this.set(undone.pop());
}