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


Embeer.js ArrayProxy invoke()用法及代碼示例


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

invoke() 方法用於在接收器中實現該方法的每個對象上調用指定方法。

用法:

invoke( methodName, args );

參數:

  • methodName:這是我們要調用的方法的名稱。
  • args: 它是一個可選參數,將傳遞該參數來調用該方法。

返回值:調用invoke的返回值。

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember server

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

ember generate route invoke1

應用程序/路由/invoke1.js


import Route from '@ember/routing/route'; 
class employee { 
    name = null; 
    city = null; 
    gender = null; 
    zipCode = null; 
    constructor(name, city, gender, zipCode) { 
        this.name = name; 
        this.city = city; 
        this.gender = gender; 
        this.zipCode = zipCode; 
    } 
    show_details() { 
        return `${this.name} is Employee from  
        ${this.city} zipCode ${this.zipCode}`; 
    } 
} 
export default class DetailsRoute extends Route { 
    details = [ 
        new employee('Anubhav', 'Patna', 'M', '800020'), 
        new employee('Satyam', 'Delhi', 'M', '110012'), 
        new employee('Shivam', 'Patna', 'M', '530068'), 
        new employee('Ayushi', 'Jaipur', 'F', '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); 
    } 
}

應用程序/控製器/invoke1.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(`Person from ${city} is ${res ? "" : 
             'not'} Present`); 
        }, 
        getFemale() { 
            let tempItems = this.details.find((i) 
            => i.gender == "F"); 
            alert(tempItems.name); 
        }, 
        show_Data() { 
            let ans = this.details.invoke('show_details'); 
            alert(ans.join('\n')) 
        }, 
    }, 
}); 

應用程序/模板/invoke1.hbs


{{page-title "invoke"}} 
<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"}} 
/> 
  
<br /><br /> 
<div> 
    <input 
        type="button"
        id="show-details"
        value="Employee's details"
        {{action "show_Data"}} 
    /> 
</div> 
{{outlet}}
  • Output: 訪問 localhost:4200/invoke1 查看輸出

Ember.js ArrayProxy 調用方法

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

ember generate route invoke2

應用程序/路由/invoke2.js


import Route from '@ember/routing/route'; 
  
class fruit { 
    name = null; 
    isFruit = null; 
    color = null; 
    constructor(name, isFruit, color) { 
        this.name = name; 
        this.isFruit = isFruit; 
        this.color = color; 
    } 
  
    Show_item() { 
        return `${this.name} is ${this.color} color`; 
  
    } 
} 
  
export default class StudentsRoute extends Route { 
    fruits = [new fruit('Orange', true, 'orange',), 
    new fruit('Apple', true, 'red'), 
    new fruit('Pineapple', true, 'Green-grey'), 
    new fruit('Banana', true, 'yellow'), 
    new fruit('Potato', false, 'brown'), 
    new fruit('Cabbage', false, 'green'), 
    new fruit('Cauli-flower', false, 'green'), 
    ]; 
    temp2; 
    temp; 
    model() { 
        return this.fruits; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('fruits', this.fruits); 
        controller.set('temp', this.temp); 
        controller.set('temp1', this.temp1); 
        controller.set('temp2', this.temp2); 
    } 
}

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


import Ember from 'ember'; 
  
export default Ember.Controller.extend({ 
    actions: { 
        find_Data() { 
            let ans = this.fruits.invoke('Show_item'); 
            alert(ans.join('\n')) 
        }, 
        insertItem(data, data1, data2) { 
            let temp = { 
                name: data, 
                isFruit: data1, 
                color: data2, 
            } 
            let ans = this.fruits.pushObject(temp); 
        }, 
    }, 
}); 

應用程序/模板/invoke2.hbs


{{page-title "Student"}} 
<h3>List of Students: </h3> 
<br /> 
<table> 
    <tr> 
        <th>Name</th> 
        <th>isFruit </th> 
        <th>Color </th> 
  
    </tr> 
    {{#each @model as |detail|}} 
        <tr> 
            <td>{{detail.name}}</td> 
            <td> {{detail.isFruit}}</td> 
            <td>{{detail.color}}</td> 
        </tr> 
    {{/each}} 
</table> 
  
<br /> 
<br /> 
<div> 
    <input 
        type="button"
        id="check-fruit"
        value="Show Items details"
        {{action "find_Data"}} 
    /> 
</div><br /><br /> 
<div> 
    <label>Enter Fruit Name: </label> 
    {{input value=this.item1}} 
</div><br /> 
<div> 
    <label>Item is Fruit: </label> 
    {{input value=this.item2}} 
</div><br /> 
<div> 
    <label>Enter Fruit color: </label> 
    {{input value=this.item3}} 
</div><br /> 
<div> 
    <input 
        type="button"
        id="insert-item"
        value="Insert Fruit"
        {{action "insertItem" this.item1  
         this.item2 this.item3}} 
    /> 
</div> 
  
{{outlet}}
  • Output: 訪問 localhost:4200/invoke2 查看輸出

Ember.js ArrayProxy 調用方法

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



相關用法


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