当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。