Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/Ember - Toggle Boolean Value of Variable

Tags:

I know that there is the .set() method to set a boolean to true or false but I want to know if there a method like .set() except it Toggles the value of a boolean.

In this case the boolean isEditing is being set to 'true'.

isEditing:false, actions: {   edit: function(category){     category.set('isEditing', true);     console.log(this.get('isEditing'))   }, } 

Here is the html

{{#if isEditing}}   <div class="category-text">      {{view Ember.TextField valueBinding=name action="turnOffEditMode"}}   </div> {{else}}   <div class="category-text">      {{#linkTo 'category' this}}       {{name}}     {{/linkTo}}   </div> {{/if}}   
like image 732
Ian Steffy Avatar asked Feb 21 '14 18:02

Ian Steffy


People also ask

How do you toggle the value of a variable in Javascript?

Method 1: Using the logical NOT operator: The logical NOT operator in Boolean algebra is used to negate an expression or value. Using this operator on a true value would return false and using it on a false value would return true. This property can be used to toggle a boolean value.

How do you toggle Boolean in Javascript?

To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.


2 Answers

this.toggleProperty('propertyName'); 

Edit: jsbin for proof

like image 150
Jake Haller-Roby Avatar answered Sep 21 '22 09:09

Jake Haller-Roby


isEditing:false, actions: {     edit: function(category){         category.set('isEditing', !category.isEditing);         console.log(this.get('isEditing'))     }     toggleEditing : function(category){     } } 

Toggling the boolean value is what you need. Or yu can change your function like below.

isEditing:false, actions: {     toggleEditing : function(category){         category.set('isEditing', !category.isEditing);         console.log(this.get('isEditing'));     } } 
like image 36
Triode Avatar answered Sep 22 '22 09:09

Triode