-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
Seems like a useful concept. What do you think of the syntax |
@dgeb Good point! I like your syntax better. |
@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. |
@wagenet thanks for the hint. |
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 |
@pangratz Would you like to DRY this up? |
I'll give it a DRY. Uhh, Oscar for bad pun, here I come.. |
isFoo?foo:bar also has the unique advantage of being the JS ternary operator. |
Clever. |
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')
}); |
@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). |
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 |
I rebased onto master and squashed the commits. |
I like this, but I think we should colons. |
I've rebased on to the latest master. The current implementation uses the ternary opertator syntax |
It looks like I wasn't rebasing on to the latest master 😔 I'm on it ... |
@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. |
thanks @dgeb for the hint |
If you make it use colon syntax, I will merge immediately :) |
The helper methods are - _parsePropertyPath - _classStringForValue - _classStringForPath
The syntax to define class names for truthy and falsy values changed from isEnabled?enabled:disabled to isEnabled:enabled:disabled
Can I get some feedback on this? |
Seems good to me, @pangratz. I appreciate the refactoring you've done to consolidate property parsing and class name evaluation. |
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 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 |
@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 @ebryn Any more concerns or are you ready to merge? |
@pangratz My intent was that |
Double colon is confusing, but I don't have a better suggestion. |
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`
@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 ... |
Support :: syntax in classNameBindings of Ember.View
Thanks @pangratz for adding this. |
Wuhuuu, nice! 🤘 Thanks for your feedback! |
Very nice, thx all of you for this feature. |
Awesome! thanks! |
I'm just curious, what was the reason for preferring the double colon syntax over the ternary |
This commit extends bindings to class names by adding support for falsy class names by using the double colon* syntax as following:
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