backbone.memento icon indicating copy to clipboard operation
backbone.memento copied to clipboard

change event does not know if it originated from Memento

Open rockymeza opened this issue 12 years ago • 3 comments

I am rather new to Backbone and Backbone.Memento and perhaps there is a better way to do what I want to do.

I am writing a view that binds to the model's change event. I want it to have a slightly different behavior if the change came from a set or a restore (which calls set).

var MyView = Backbone.View.extend({
  initialize: function() {
    this.model.bind('change', this.doChange, this);
  },

  doChange: function(model, options) {
    if ( this_was_a_restore )
      do_it_like_this();
    else
      do_it_like_that();
  }
});

Right now, I have hacked on a setOptions config entry in the main config that gets passed to the Serializer. I modified the restore method on the TypeHelper to pass those setOptions when it calls structure.set. The Serializer passes these options in when it calls typeHelper.restore. I can set the setOptions config at instantiation or when I restore my model.

My model looks something like this:

var MyModel = Backbone.Model.extend({
  initialize: function() {
    var memento = new Backbone.Memento(this, {setOptions: {restoring: true}});
    _.extend(this, memento);
  }
});

And my view's doChange method can now check the options passed to it:

doChange: function(model, options) {
  if ( options.restoring )
    do_it_this_way();
  else
    do_it_that_way();
}

So I guess my question is, are there ways of doing this that already exist or are there better ways of doing it? If not, is this something that you think other people would want to use or worth sticking into Memento? If so, I can and the code to the issue so you can merge it in.

I'm not sure if setOptions is a great name, considering it could be passed to Model.unset or Collection.remove or Collection.reset.

Thanks for Memento, btw, it makes for some cool things.

rockymeza avatar Nov 01 '11 05:11 rockymeza

Sorry for writing the issue without a pull request.

I made a pull request (derickbailey/backbone.memento#6), but I'm not sure how to combine them.

rockymeza avatar Nov 08 '11 17:11 rockymeza

:+1:

russplaysguitar avatar Sep 10 '14 16:09 russplaysguitar