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

change event does not know if it originated from Memento #6

Open
rockymeza opened this issue Nov 1, 2011 · 3 comments
Open

change event does not know if it originated from Memento #6

rockymeza opened this issue Nov 1, 2011 · 3 comments

Comments

@rockymeza
Copy link

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 rockymeza mentioned this issue Nov 8, 2011
@rockymeza
Copy link
Author

Sorry for writing the issue without a pull request.

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

@russplaysguitar
Copy link

👍

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

3 participants
@rockymeza @russplaysguitar and others