mxriverlynn / backbone.memento

store and restore your model's state
367 stars 51 forks source link

Rebasing memento on current state #23

Open StevenLangbroek opened 9 years ago

StevenLangbroek commented 9 years ago

We're using Memento in combination with Stickit to drive our forms. In this particular case, we have a collection, which allows editing of a single model at a time. When you navigate away, the changes are obviously still there in the model, so when you come back to edit this model, you'll see the old state. This is all expected behavior, but here's where I'm running into a limitation:

Would it be useful to have a method to set a new "starting point" so people can't restore to beyond that point, or have a configuration option to automatically do that when an entity is persisted?

mxriverlynn commented 9 years ago

interesting timing... i blogged about decorating a model w/ memento just a few minutes ago: http://derickbailey.com/2014/09/09/decorating-a-backbone-model-with-features-for-a-ui-component/

in your situation, this could work out the way you want without having to modify memento in any way. rather than having memento baked in to the model directly, you could use a decorator to add the feature where you need it.

StevenLangbroek commented 9 years ago

We're already doing that. I'm using Cocktail to mix this behavior in:

define (require) ->

  Backbone = require 'backbone'
  require 'backbone.memento'

  Memento =
    initialize: (options) ->
      memento = new Backbone.Memento @
      _.extend @, memento

    reject: ->
      @restore()

My problem is that there's no way to stop somebody from restoring past a certain "version". So when someone clicks "save", I can disable the "cancel" button when the model's sync comes back, but when they make some changes, the button becomes available again (enabled on model:change), but there's no way to really make sure they don't roll back another version to before the save call. I can semi-enforce this in the UI, but it would make more sense to me to enforce this in Memento in some way than adding (potentially) complex UI logic.

mxriverlynn commented 9 years ago

ok. probably should add something like that in to memento...

out of curiosity, where is that code happening, for the mixin? are you allowing people to make multiple edits / saves before moving away from the edit screen? In my own use of the decorator w/ memento, whenever the edit screen closes, the memento goes away and therefore cannot be rolled back any further.

StevenLangbroek commented 9 years ago

In this case, saving doesn't move you away from the edit screen. We have a collection of models, each has its own tab, but there is no "list" to go back to when saving, so you stay in the same screen and get a notification "flash" on top of the screen when save is successful.