Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebasing memento on current state #23

Open
StevenLangbroek opened this issue Aug 29, 2014 · 4 comments
Open

Rebasing memento on current state #23

StevenLangbroek opened this issue Aug 29, 2014 · 4 comments

Comments

@StevenLangbroek
Copy link

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:

  • Collection is instantiated, each model calls memento.store() so we have initial state.
  • User edits an attribute, click save. I could at this point create a new state.
  • If user navigates away or presses cancel, we can revert 1 step, but there's no real way of blocking the user from pressing cancel again, thus reverting back to beyond the saved state.

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants