Open In App

Ember.js ArrayProxy every() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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 a large number of websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.

The every() method is used to check all the items of the array against the function.

Syntax:

every( callback, target );

 

Parameters:

  • callback: It is the callback function that is tested against each item of the array.
  • target: It is the item that is tested against the callback function.

Return Value: It returns true if all item passes the function else returns false.

Steps to Install and Run Ember.js:

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

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 every1

app/routes/every1.js




import Route from '@ember/routing/route';
import { } from '@ember/array';
  
export default class FruitsRoute extends Route {
    item1 = [
        {
            name: 'Apple',
            isFruit: true,
            color: 'red',
        },
        {
            name: 'Grapes',
            isFruit: true,
            color: 'green',
        },
        {
            name: 'Mango',
            isFruit: true,
            color: 'yellow',
        },
        {
            name: 'Watermelon',
            isFruit: true,
            color: 'red',
        },
        {
            name: 'Orange',
            isFruit: true,
            color: 'orange',
        }
    ];
    item2 = [
        {
            name: 'Lady Finger',
            isFruit: false,
            color: 'green',
        },
        {
            name: 'Brinjal',
            isFruit: false,
            color: 'purple',
        },
        {
            name: 'Potato',
            isFruit: false,
            color: 'brown',
        },
        {
            name: 'Onion',
            isFruit: false,
            color: 'violer',
        },
    ];
    model() {
        return this.item1;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('item1', this.item1);
        controller.set('item2', this.item2);
    }
}


app/controllers/every1.js




import Ember from 'ember';
import { every, addObjects } from '@ember/array';
  
export default Ember.Controller.extend({
    actions: {
        All_fruit() {
            let foo = this.item1.every((item) =>
              item.isFruit == true);
            alert(foo ? 'Yes all items are Fruit'
             : 'No All items not Fruit');
        },
        Any_Vegy() {
            let foo = this.item1.every((item) => 
            item.isFruit == false);
            alert(foo ? 'Yes all items are vegetable' 
            : 'No All items Not Vegetable');
        },
        AddItems() {
            this.item1.addObjects(this.item2);
        }
  
    },
});


app/templates/every1.hbs




{{page-title "Fruits"}}
<table style=" border-spacing : 30px">
    <h3>Here is a list 1: </h3>
    <ul>
        {{#each @model as |eatable|}}
            <li>{{eatable.name}}</li>
        {{/each}}
    </ul>
</table>
<input
    type="button"
    id="all-Fruit"
    value="All items are Fruit"
    {{action "All_fruit"}}
/>
  
<br /><br />
<input
    type="button"
    id="any-vegy"
    value="All item are Vegetables"
    {{action "Any_Vegy"}}
/>
  
<br /><br />
<input type="button" id="add-items"
  value="Add Items" {{action "AddItems"}} />
  
{{outlet}}


Output: Visit localhost:4200/every1 to view the output

Ember.js ArrayProxy every method

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

ember generate route every2

app/routes/every2.js




import Route from '@ember/routing/route';
  
class student {
    name = null;
    gender = null;
    clas = null;
    grade = null;
    constructor(name, gender, clas, grade) {
        this.name = name;
        this.gender = gender;
        this.clas = clas;
        this.grade = grade;
    }
}
  
export default class StudentsRoute extends Route {
    students = [
        new student('Aakash', 'M', 10, 'A'),
        new student('Soniya', 'F', 8, 'C'),
        new student('Esnoor', 'M', 9, 'C'),
        new student('Isha', 'F', 11, 'B'),
        new student('Doman', 'M', 12, 'B'),
        new student('Lolu', 'M', 10, 'A'),
        new student('Satyam', 'M', 10, 'A'),
    ];
    temp2;
    temp;
    model() {
        return this.students;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('students', this.students);
        controller.set('temp', this.temp);
        controller.set('temp1', this.temp1);
        controller.set('temp2', this.temp2);
    }
}


app/controllers/every2.js




import Ember from 'ember';
import { mapBy, every } from '@ember/array';
  
export default Ember.Controller.extend({
    actions: {
        Every_Pass() {
            let foo = this.students.every((person) =>
             person.grade != 'F')
            alert(foo ? "Yes Everyone Pass" :
             "No Everyone not Pass");
        },
        Every_Female() {
            let ans = this.students.mapBy('gender');
            let temp = ans.filter((gen) => 
             gen == 'F');
            alert(`Total girls in list is
             ${temp.length} `)
        },
        Every_male() {
            let ans = this.students.mapBy('gender');
            let temp = ans.filter((gen) =>
             gen == 'M');
            alert(`Total girls in list is
             ${temp.length} `)
        },
    },
});


app/templates/every2.hbs




{{page-title "Student"}}
<h3>List of Students: </h3>
<br /><br />
<table>
    <tr>
        <th>Name</th>
        <th>Gender </th>
        <th>Class </th>
        <th>Grade </th>
    </tr>
    {{#each @model as |detail|}}
        <tr>
            <td>{{detail.name}}</td>
            <td> {{detail.gender}}</td>
            <td>{{detail.clas}}</td>
            <td>{{detail.grade}}</td>
        </tr>
    {{/each}}
</table>
  
<br />
<br />
<div>
    <input
        type="button"
        id="check-pass"
        value="Is Every One Pass"
        {{action "Every_Pass"}}
    />
</div>
<br />
<div>
    <input
        type="button"
        id="check-female"
        value="Total Female Student"
        {{action "Every_Female"}}
    />
</div>
<br />
<div>
    <input
        type="button"
        id="check-male"
        value="Total Male Student"
        {{action "Every_male"}}
    />
</div>
{{outlet}}


Output: Visit localhost:4200/every2 to view the output

Ember.js ArrayProxy every method

Reference: https://api.emberjs.com/ember/4.4/classes/ArrayProxy/methods/every?anchor=every



Last Updated : 31 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads