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

Support :: syntax in classNameBindings of Ember.View #732

Merged
merged 13 commits into from
Jul 18, 2012
Merged

Support :: syntax in classNameBindings of Ember.View #732

merged 13 commits into from
Jul 18, 2012

Conversation

pangratz
Copy link
Member

This commit extends bindings to class names by adding support for falsy class names by using the double colon* syntax as following:

Ember.View.create({
  classNameBindings: ['isEnabled:enabled:disabled']
  isEnabled: true
});

This will add the class 'enabled' when isEnabled is true. If isEnabled is false the class 'disabled' is added instead.

*Inspired by recent semicolon discussion

@travisbot
Copy link

This pull request fails (merged b4c784b4 into 3c02542).

@dgeb
Copy link
Member

dgeb commented Apr 24, 2012

Seems like a useful concept. What do you think of the syntax isEnabled?enabled:disabled to show that it's acting as a ternary statement? I suppose isEnabled?enabled would also need to be supported for consistency.

@pangratz
Copy link
Member Author

@dgeb Good point! I like your syntax better.

@wagenet
Copy link
Member

wagenet commented Apr 24, 2012

@pangratz This also needs to be added to the Handlebars helpers as well. However, we should make sure this is something we do want to add before you go to the trouble to add it there also.

@pangratz
Copy link
Member Author

@wagenet thanks for the hint.

@pangratz
Copy link
Member Author

Just a note: there should be a single method which returns the class name for a property and this should be used in both places to DRY: binding.js and view.js

@wagenet
Copy link
Member

wagenet commented Apr 24, 2012

@pangratz Would you like to DRY this up?

@pangratz
Copy link
Member Author

I'll give it a DRY. Uhh, Oscar for bad pun, here I come..

@devinus
Copy link
Member

devinus commented Apr 26, 2012

isFoo?foo:bar also has the unique advantage of being the JS ternary operator.

@devinus
Copy link
Member

devinus commented Apr 26, 2012

Clever.

@knassar
Copy link

knassar commented Apr 26, 2012

I think the more flexible solution would be to allow attr/class bindings to a non-boolean computed property, with the return value of the property being used as the attribute value. Something like:

Ember.View.create({
  classNameBindings: ['elementClasses::'],
  isEnabled: true,
  elementClasses: Ember.computed(function() {
    if (this.get('isEnabled') === true) {
       return "enabled";
    } else if (this.get('isEnabled') === false) {
       return "disabled";
    } else {
       return "";
    }
  }).property('isEnabled')
});

@dgeb
Copy link
Member

dgeb commented Apr 26, 2012

@knassar - Class names can currently be bound to computed properties. Here's an example I modified: http://jsfiddle.net/dgeb/hscQV/

I think the advantage of @pangratz's ternary solution is its brevity. Plus, it can be used from either the view class or the handlebars template (although #593 still needs to be sorted out).

@knassar
Copy link

knassar commented Apr 26, 2012

Huh. I could have sworn I tried that and it didn't work. Thanks @dgeb

Well, in that case, I like the ternary operator version: property?trueClass:falseClass

@travisbot
Copy link

This pull request passes (merged 994dd8be into 4ef1568).

@travisbot
Copy link

This pull request passes (merged 9c600f24 into 4ef1568).

@pangratz
Copy link
Member Author

I rebased onto master and squashed the commits.

@travisbot
Copy link

This pull request passes (merged c9cf4ab into 4ef1568).

@dgeb
Copy link
Member

dgeb commented May 17, 2012

Since this has a large amount of overlap with code I'm working on to finally wrap up #593, it would be nice to get this accepted sooner than later (of course, assuming it's acceptable :). Otherwise, either @pangratz or myself will end up doing a bunch of refactoring afterward.

@ebryn
Copy link
Member

ebryn commented Jun 2, 2012

I like this, but I think we should colons.

@pangratz
Copy link
Member Author

I've rebased on to the latest master. The current implementation uses the ternary opertator syntax path?true-class:false-class. But changing it to double-colon syntax should be an easy change. So what do you think? Any comments on the implementation?

@pangratz
Copy link
Member Author

It looks like I wasn't rebasing on to the latest master 😔

I'm on it ...

@dgeb
Copy link
Member

dgeb commented Jul 14, 2012

@pangratz please take a look at how class bindings are now evaluated in the {{view}} helper. This will require some changes, regardless of which format (:: vs ?:) is chosen. I still like the ternary format, but I may be in the minority here.

@pangratz
Copy link
Member Author

thanks @dgeb for the hint

@ebryn
Copy link
Member

ebryn commented Jul 14, 2012

If you make it use colon syntax, I will merge immediately :)

@travisbot
Copy link

This pull request passes (merged b7aa61c into 3d8a3b3).

The syntax to define class names for truthy and falsy values changed from isEnabled?enabled:disabled to isEnabled:enabled:disabled
@travisbot
Copy link

This pull request passes (merged 89d6319 into 3d8a3b3).

@travisbot
Copy link

This pull request passes (merged b0d4845 into 3d8a3b3).

@travisbot
Copy link

This pull request passes (merged 0e41ab5b into 3d8a3b3).

@travisbot
Copy link

This pull request passes (merged 96840c6 into 3d8a3b3).

@travisbot
Copy link

This pull request passes (merged 0d384ae into 3d8a3b3).

@pangratz
Copy link
Member Author

Can I get some feedback on this?

@dgeb
Copy link
Member

dgeb commented Jul 18, 2012

Seems good to me, @pangratz. I appreciate the refactoring you've done to consolidate property parsing and class name evaluation.

@pangratz
Copy link
Member Author

Thanks @dgeb !

I was thinking about another thing: sometimes people ask for a binding where a class is bound if the value of the property is false. Previously you had to create a computed property or a Ember.Binding.not() for that:

Ember.View.extend({
   isNotEnabledBinding: Ember.Binding.not('isEnabled'),
   classNameBindings: ['isNotEnabled:not-enabled']
});

When this gets merged, it would be possible via:

Ember.View.extend({
   classNameBindings: ['isEnabled::not-enabled']
});

Is this something which should be supported or should I add an Ember.assert for the "true class" to not being empty?

@dgeb
Copy link
Member

dgeb commented Jul 18, 2012

@pangratz I discussed this very question with @ebryn a few days ago and we agreed it's convenient to allow the true class to be empty so that isEnabled::not-enabled works (although I find the syntax a bit weird, especially coming from Ruby). So I'm glad this works - thanks for checking!

@ebryn Any more concerns or are you ready to merge?

@ebryn
Copy link
Member

ebryn commented Jul 18, 2012

@pangratz My intent was that isEnabled::not-enabled would work.

@devinus
Copy link
Member

devinus commented Jul 18, 2012

Double colon is confusing, but I don't have a better suggestion.

@pangratz
Copy link
Member Author

Thanks for the feedback!

I'll add a test case covering the empty true class ...

So if a binding like

Ember.View.extend({
  classNameBindings: ['isEnabled::disabled']
});

adds no class if `isEnabled` is `true` and adds the class disabled when `isEnabled` is `false`
@pangratz
Copy link
Member Author

@ebryn I've added a test case for the empty true class case.

I'll do a rebase on master if this is good to go ...

@travisbot
Copy link

This pull request passes (merged ae0dd6a into 3d8a3b3).

@travisbot
Copy link

This pull request passes (merged 7df746d into 3d8a3b3).

ebryn added a commit that referenced this pull request Jul 18, 2012
Support :: syntax in classNameBindings of Ember.View
@ebryn ebryn merged commit 0a4ece1 into emberjs:master Jul 18, 2012
@ebryn
Copy link
Member

ebryn commented Jul 18, 2012

Thanks @pangratz for adding this.

@pangratz
Copy link
Member Author

Wuhuuu, nice! 🤘

Thanks for your feedback!

@sly7-7
Copy link
Contributor

sly7-7 commented Jul 18, 2012

Very nice, thx all of you for this feature.

@albertjan
Copy link

Awesome! thanks!

@sherwinyu
Copy link
Contributor

I'm just curious, what was the reason for preferring the double colon syntax over the ternary test?trueClass:falseClass syntax?

@pangratz pangratz deleted the add_double_colon_syntax branch November 14, 2013 19:19
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

Successfully merging this pull request may close these issues.

10 participants