简体   繁体   English

Javascript / Ember - 切换变量的布尔值

[英]Javascript/Ember - Toggle Boolean Value of Variable

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. 我知道有.set()方法将布尔值设置为true或false但我想知道是否有类似.set()的方法,除非它切换布尔值。

In this case the boolean isEditing is being set to 'true'. 在这种情况下,布尔值isEditing被设置为“true”。

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

Here is the html 这是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}}  
this.toggleProperty('propertyName');

编辑: jsbin为证明

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'));
    }
}

@gravityplanx's answer is great. @ gravityplanx的答案很棒。

I just want to add that this methodology works beyond the controller as well. 我只想补充一点,这种方法也适用于控制器之外。

For example, if my Model has a Post object, I can update it from the controller. 例如,如果我的Model有一个Post对象,我可以从控制器更新它。 I just have to pass the object into a controller action. 我只需要将对象传递给控制器​​动作。

switch_readiness(post) {
    post.toggleProperty('isPublishReady');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM