當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Embeer.js EmberArray any()用法及代碼示例


Ember.js 是一個開源 JavaScript 框架,用於開發基於 Model-View-Controller (MVC) 架構的大型客戶端 Web 應用程序。 Ember.js是使用最廣泛的前端應用框架之一。它的目的是加速開發並提高生產力。目前,它被大量網站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

any()方法用於檢查目標中是否有任何對象與回調函數設定的條件相匹配。

用法:

any(callback, target)

參數:

  • callback:它是應用於目標的每個對象的回調函數。
  • target: 這是必須檢查的目標對象。

返回:如果有任何對象匹配則為 true,否則為 false。

第 1 步:要運行以下示例,您需要有一個 ember 項目。要創建一個,您需要先安裝ember-cli。在終端中寫入以下代碼:

npm install ember-cli

第 2 步:現在您可以通過輸入以下代碼來創建項目:

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

示例 1:鍵入以下代碼以生成本示例的路由:

ember generate route Any1

應用程序/路線/Any1.js


import Route from '@ember/routing/route'; 
  
export default class DetailsRoute extends Route { 
    details = [ 
        { 
            name: 'Anubhav', 
            city: 'Patna', 
            gender: 'M', 
            zipCode: '800020', 
        }, 
        { 
            name: 'Satyam', 
            city: 'Delhi', 
            gender: 'M', 
            zipCode: '110012', 
        }, 
        { 
            name: 'Shivam', 
            city: 'Patna', 
            gender: 'M', 
            zipCode: '530068', 
        }, 
        { 
            name: 'Ayushi', 
            city: 'Jaipur', 
            gender: 'F', 
            zipCode: '302001', 
        }, 
    ]; 
    attr = ['name', 'city', 'gender', 'zipCode']; 
    temp; 
    start; 
    end; 
    model() { 
        return this.details; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('details', this.details); 
        controller.set('attr', this.attr); 
        controller.set('temp', this.temp); 
        controller.set('temp2', this.temp2); 
        controller.set('end', this.end); 
    } 
}

應用程序/控製器/Any1.js


import Ember from "ember"; 
import { any, slice, reverseObjects, rejectBy } 
    from "@ember/array"; 
  
export default Ember.Controller.extend({ 
    actions: { 
        checkCity(city) { 
            let res = this.details.any((person) => 
                person.city == city); 
            alert(`${city} is ${res ? "" : 'not'} Present`); 
        }, 
        getFemale() { 
            let tempItems = this.details.find((i) => i.gender == "F"); 
            alert(tempItems.name); 
        } 
    }, 
});

應用程序/模板/Any1.hbs


{{page-title "Any1"}} 
<h3>List of People: </h3> 
<br /><br /> 
<table> 
    <tr> 
        <th>Name</th> 
        <th>Gender</th> 
        <th>City</th> 
        <th>Zip Code</th> 
    </tr> 
    {{#each @model as |detail|}} 
    <tr> 
        <td>{{detail.name}}</td> 
        <td>{{detail.gender}}</td> 
        <td>{{detail.city}}</td> 
        <td>{{detail.zipCode}}</td> 
    </tr> 
    {{/each}} 
</table> 
<br /><br /> 
<div> 
    <label>Enter City: </label> 
    {{input value=this.city}} 
</div> 
<div> 
    <input type="button" id="check-city" 
        value="Check" 
        {{action 'checkCity' this.city}} /> 
</div> 
<br/> 
<input type="button" id="get-female" 
    value="Get Female" {{action 'getFemale' }} /> 
  
{{outlet}}

輸出:訪問 localhost:4200/Any1 查看輸出

Ember.js EmberArray 任何方法

示例 2:鍵入以下代碼以生成本示例的路由:

ember generate route Any2

應用程序/路線/Any2.js


import Route from '@ember/routing/route'; 
import { } from '@ember/array'; 
  
export default class FruitsRoute extends Route { 
    fruits = [ 
        { 
            name: 'Lady Finger', 
            isFruit: false, 
            isVegy: true, 
            color: 'green', 
        }, 
        { 
            name: 'Tomato', 
            isFruit: false, 
            isVegy: true, 
            color: 'red', 
        }, 
        { 
            name: 'Apple', 
            isFruit: true, 
            isVegy: false, 
            color: 'red', 
        }, 
        { 
            name: 'Potato', 
            isFruit: false, 
            isVegy: true, 
            color: 'brown', 
        }, 
        { 
            name: 'Mango', 
            isFruit: true, 
            isVegy: true, 
            color: 'yellow', 
        }, 
        { 
            name: 'Bannana', 
            isFruit: true, 
            isVegy: false, 
            color: 'yellow', 
        }, 
        { 
            name: 'Cabbage', 
            isFruit: false, 
            isVegy: true, 
            color: 'green', 
        }, 
        { 
            name: 'Orange', 
            isFruit: true, 
            isVegy: false, 
            color: 'orange', 
        }, 
    ]; 
    temp1; 
    temp2; 
    model() { 
        this.temp1 = this.fruits.filterBy('isFruit', false); 
        this.temp2 = this.fruits.filterBy('isFruit', true); 
  
        return this.fruits; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('fruits', this.fruits); 
        controller.set('temp1', this.temp1); 
        controller.set('temp2', this.temp2); 
    } 
}

應用程序/控製器/Any2.js


import Ember from "ember"; 
import { shiftObject, Any, isEvery } from "@ember/array"; 
  
export default Ember.Controller.extend({ 
    actions: { 
        anyFruit() { 
            this.fruits.any((i) => i.isFruit == true) ?  
            alert('Fruit Present in list ') :  
            alert("Fruit Not present in list"); 
        }, 
        allFruit() { 
            this.fruits.any((i) => i.isVegy == true) ? 
            alert('Vegitable Present in list') :  
            alert("Vegitable Not present in list"); 
        }, 
    }, 
});

應用程序/模板/Any2.hbs


{{page-title "Fruits"}} 
<h3>Here is a list of eatables: </h3> 
<ul> 
    {{#each @model as |eatable|}} 
        <li>{{eatable.name}}</li> 
      {{/each}} 
</ul> 
<br /><br /> 
<input 
    type="button"
     id="fruit-any"
     value="List Contains Any Fruit?"
      {{action "anyFruit"}} 
/> 
<br /><br /> 
<input 
    type="button"
    id="fruit-all"
    value="List Contains Only Fruit?"
    {{action "allFruit"}} 
/> 
{{outlet}}

輸出:訪問 localhost:4200/Any2 查看輸出

Ember.js EmberArray 任何方法

參考:https://api.emberjs.com/ember/4.6/classes/EmberArray/methods/any?anchor=any



相關用法


注:本文由純淨天空篩選整理自satyam00so大神的英文原創作品 Ember.js EmberArray any() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。