derickbailey/backbone.memento — Issue #6: change event does not know if it originated from Memento
Author: rockymeza
Created: 2011-11-01 05:11:19+00:00
State: open

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).

``` javascript
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:

``` javascript
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:

``` javascript
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.