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

TextBinding unbind needs to add original el.textContent back in place #623

Open
anshul-kai opened this issue May 27, 2016 · 6 comments
Open

Comments

@anshul-kai
Copy link

The TextBinding template needs to be reset upon unbind. This comes in handy when dealing with TextBinding in a nested view. I've demonstrated this by explicitly unbinding the view in the JSFiddle below.

https://jsfiddle.net/havessex/1/

@Duder-onomy
Copy link
Collaborator

@tanshul22 Your example is pretty cool, but very complex.

Can you boil it down to just a rv-text that is bound, then unbound? And tell us what behavior you have an issue with?

I am interested in helping you. But your example is much too complex to isolate rivets alone as the issue.

@anshul-kai
Copy link
Author

anshul-kai commented Aug 23, 2016

Thanks for looking into this @Duder-onomy. This issue is exasperated by #622 and thus the update to rv-if.unbind was added to my original example.

I've created a much simpler version of the bug here. As you'll notice rv-text performs fine upon a re-bind but TextBinding does not.

The solution that works for me is to unbind a TextBinding as follows (using version 0.8.1):

    TextBinding.prototype.unbind = function() {
      var _ref1, _ref2, _ref3, _results;
      _ref1 = this.view.templateDelimiters;
      _ref2 = this.options.dependencies ? ' < ' + this.options.dependencies.join(' ') : '';
      _ref3 = this.options.formatters.length ? ' | ' + this.options.formatters.join(' | ') : '';
      _results = TextBinding.__super__.unbind.apply(this, arguments);
      this.el.textContent = _ref1[0] + this.keypath + _ref2 + _ref3 + _ref1[1];
      return _results;
    };

@jccazeaux
Copy link
Contributor

I'm curious : what is the use case where you need to unbind the view and rebind it later? When i unbind a view it's to overwrite the template before performing a new bind

Nevertheless your solution will work, but I would do it like this for simplicity reasons

class Rivets.TextBinding extends Rivets.Binding
  constructor: (@view, @el, @type, @keypath, @options = {}) ->
    [...]
    @originalTextContent = @el.textContent

  unbind: =>
    super
    @el.textContent = @originalTextContent

I did not test it, but it should do the job.

@anshul-kai
Copy link
Author

Hi @jccazeaux. I like your suggestion of saving off the originalTextContent. I'll give that a shot.

To answer your question, the real problem is better described in the original example which wraps up the text elements inside an rv-if which in my application often gets bound and unbound. Hope that clarifies the concern.

@jccazeaux
Copy link
Contributor

I updated your fiddle here
Basically i replaced unbind with application.view.isFullScreen = false to force rv-if to unbind. In this scenario there is no bug.
It makes sense to reset the template on unbind but we must be sure to not reset it too soon.

@anshul-kai
Copy link
Author

You're absolutely right! I can no longer recreate the original bug in my app. I have several nested if blocks and this was a corner case. I did my best by replicating it in the original fiddle but the unbind in the fiddle was just a simulation of an rv-if unbinding.

I'll keep experimenting and keep this thread updated with my findings. Thanks for helping out.

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