Open In App

Ember.js Ember.Templates.helpers mut() Method

Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by many websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.

The mut() method is used to clearly specify that the child component can update the passed to it. 



Syntax:

{{ mut attr }}

Parameters:



Steps to Install and Run Ember.js:

Step 1: To run the following examples you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:

npm install ember-cli

Step 2: Now you can create the project by typing in the following piece of code:

ember new <project-name> --lang en

To start the server, type:

ember serve

Example 1: Type the following code to generate the route for this example:

ember generate route mut1

app/component/list2.js




import Component from '@glimmer/component';
import { action, set } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import Ember from 'ember';
 
export default class ListComponent extends Component {
    @tracked
    value = 'GeeksforGeeks';
}

app/components/list2.hbs




<h2>{{unbound this.value}} is not changeable.</h2>
<h2>{{this.value}} is changeable.</h2>
<input
    type="button"
      id="set-code"
      value="Change value"
      {{action (mut this.value) "GFG"}}
/>

app/templates/mut1.hbs




{{page-title "List2"}}
<List2 @value={{ this.val }}></List2>

Output:

Ember.js Ember.Templates.helpers mut() Method

Example 2: Type the following code to generate the route for this example:

ember generate route mut2

app/components/arr.js




import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action, addObjects } from '@ember/object';
export default class CompactComponent extends Component {
    items = [
        'Oxygen',
        'Source Code',
        'Infine',
        'Tenet',
        'SpiderHead',
        'The Thing',
        'A Quiet Place',
        'The Invisible Man',
        'Looper',
        'Ad Astra',
    ];
 
    @action
    remove(data) {
        return this.items.without(data);
    }
}

app/components/arr.hbs




{{#if @show}}
    <ul>
        <h3>{{yield}} : </h3>
        {{#each this.items as |party|}}
              <li>{{unbound party}}</li>
        {{/each}}
      </ul>
      <br />
      {{textarea value=this.value}}
      <br />
      <input
        type="button"
        id="check-atIndex"
        value="Remove"
        {{action (mut this.items) (this.remove this.value)}}
      />
      <br /><br />
{{else}}
    This component Don't Have block!!<br />
{{/if}}

app/templates/mut2.js




<Arr @show={{true}}>
    List of items
</Arr>
{{outlet}}

Output:

Ember.js Ember.Templates.helpers mut() Method

Reference: https://api.emberjs.com/ember/4.6/classes/Ember.Templates.helpers/methods/mut?anchor=mut


Article Tags :