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

Add ability to select the views $el via the selector #17

Closed
asciidisco opened this issue Oct 30, 2012 · 1 comment
Closed

Add ability to select the views $el via the selector #17

asciidisco opened this issue Oct 30, 2012 · 1 comment

Comments

@asciidisco
Copy link

Hi,
first of all, thanks for this. This is the first sane approach & model <-> view bindings in backbone i´ve seen so far.

I recently was in the situation that i needed to change the 'view.el' elements class,
after digging into the sources i was not able to find a way to do that (maybe I missed smth.)

So, i added a 'custom selector' named ':el', by changing line 51 from

$el = self.$(selector);

to

$el = selector = ':el' ? self.$el : self.$(selector);

Now you can use your bindings to do smth. like this:

bindings: {
    ':el': {
        attributes: [{
            name: 'class',
            observe: 'hidden',
            format: 'toggleVisibility'
        }]
    }
}

Your thoughts?
Would love to see this in core.

Regards
Sebastian

@delambo
Copy link
Member

delambo commented Nov 3, 2012

@asciidisco This a great idea - I'm surprised I haven't needed this yet! I pushed this into master and I'll cut a tag and write some docs soon.

In your example, it looks like you are toggling visibility on the element. If so, I recently pushed some api changes to master which may help, or at least be more intuitive. For example:

bindings: {
  ':el': {
    modelAttr: 'hidden',
    // Turns on show/hide toggling, which will be toggled based on the `hidden` attributes change in truthiness.
    // May also define a callback function which should return true or false after the `hidden` attribute is changed.
    visible: true,
    // Optionally, if you want a better animation than the standard jQuery show()/hide(), you can define the
    // following key and callback, and manually show and hide the bound element.
    visibleFn: function($el, isVisible, attrName) {
      if (isVisible) $el.fadeIn('fast');
      else $el.fadeOut('fast');
    }
}

@delambo delambo closed this as completed Nov 3, 2012
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