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

Can you bind multiple views to the same model? #227

Open
SnidelyWhiplash opened this issue May 27, 2014 · 4 comments
Open

Can you bind multiple views to the same model? #227

SnidelyWhiplash opened this issue May 27, 2014 · 4 comments

Comments

@SnidelyWhiplash
Copy link

I'm using Marionette, but it shouldn't make a difference as far as this question.

I have two views, a LoginView and a SignupView (both forms). They are shown at the same time, and instantiated with an empty User model, eg:

onRender: function () {
  var user = new User();
  this.login.show(new LoginView({model: user}));
  this.signup.show(new SignupView({model: user}));
}

each of those views is doing

initialize: function () {
  Backbone.Validation.bind(this);
}

but only the SignupView is seeing the validation effects. Is there a way to have the model validation affect both views, or am I going to have to send them each a separate Model instance?

Thanks for the awesome plugin!

@SomethingSexy
Copy link

I have done this in the past. Don't bind directly to the view but use the mixin on the model instead.

@SnidelyWhiplash
Copy link
Author

Thanks for the reply. So then you just listen for the vaildated:* events on the model within the view directly..?

@SomethingSexy
Copy link

Yep, you can just have your view listenTo the validated events on the model, then wire up your UI interactions that way. We ended up need something a little bit more robust so we actual manually call the preValidation method on the model for what we are trying to validate (because we are doing real-time validation) and then we do whatever we need based on valid/invalid.

I might also suggest making some sort of view mixin so that you can mixin in that with any view you need to handle model validation.

@SnidelyWhiplash
Copy link
Author

Sounds good, thanks for the rundown. For clarification on one point, I thought preValidate() didn't fire the events..? Does it even set _isValid on the model..? Going off memory for now, I can dive into the code to determine. Thanks!

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