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


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


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

objectAt() 方法用於檢索給定索引處的對象。如果我們使用負數或大於數組大小的數字,則此方法返回未定義。

用法:

objectAt(index)

參數:

  • index: 它是我們想要的元素的索引。

返回值:索引處的項目。

安裝和運行 Ember.js 的步驟:

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route objectAt1

應用程序/路線/objectAt1.js


import Route from '@ember/routing/route'; 
  
class student { 
    name = null; 
    gender = null; 
    class = null; 
    grade = null; 
    constructor(name, gender, class, grade) { 
        this.name = name; 
        this.gender = gender; 
        this.class = class; 
        this.grade = grade 
    } 
  
    show_grade() { 
        return `I am ${this.name} of  
        Class ${this.class} Get ${this.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); 
    } 
}

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


import Ember from 'ember'; 
import { mapBy } from '@ember/array'; 
  
export default Ember.Controller.extend({ 
    actions: { 
        removeDetails(start,end) { 
            this.students.removeAt(start,end); 
          }, 
          getDetail(index) { 
            let foo = this.students.objectAt(index); 
            alert(`${foo.name} of class ${foo.class}`); 
          }, 
  
    }, 
});

應用程序/模板/objectAt1.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.class}}</td> 
            <td>{{detail.grade}}</td> 
        </tr> 
    {{/each}} 
</table> 
  
<br /> 
<br /><div> 
    <label>Enter Start Index: </label> 
    {{input value=this.start}} 
</div> 
<div> 
    <label>Enter End Index: </label> 
    {{input value=this.end}} 
</div> 
<div> 
    <input 
        type="button"
        id="remove-details"
        value="Remove Details"
        {{action "removeDetails"
         this.start this.end}} 
    /> 
</div> 
<br /><br /> 
<div> 
    <label>Enter Index: </label> 
    {{input value=this.idx}} 
</div> 
<div> 
    <input 
        type="button"
        id="get-detail"
        value="Get detail"
        {{action "getDetail" this.idx}} 
    /> 
</div> 
{{outlet}}

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

Ember.js EmberArray objectAt 方法

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

ember generate route objectAt2

應用程序/路線/objectAt2.js


import Route from '@ember/routing/route'; 
import { } from '@ember/array'; 
  
export default class FruitsRoute 
    extends Route { 
    item1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
    item2 = ['l', 'm', 'h', 'i', 'j', 'k']; 
    model() { 
        return this.item1; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('item1', this.item1); 
        controller.set('item2', this.item2) 
    } 
}

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


import Ember from 'ember'; 
import { removeAt, unshiftObjects, objectAt, 
        uniqBy, find } from '@ember/array'; 
  
export default Ember.Controller.extend({ 
    actions: { 
        Show_item(start, end) { 
            let foo = this.item1.objectAt(start); 
            alert(foo); 
        }, 
        Push_item() { 
            this.item1.pushObjects(this.item2) 
        }, 
    }, 
}); 

應用程序/模板/objectAt2.hbs


{{page-title "Fruits"}} 
<table style=" border-spacing : 30px"> 
<h3>Here is a list 1: </h3> 
<ul> 
    {{#each @model as |item|}} 
    <li>{{item}}</li> 
    {{/each}} 
</ul> 
</table> 
<div> 
    <label>Enter Start Index: </label> 
    {{input value=this.start}} 
</div> 
<div> 
    <input type="button"
        id="show-details"
        value="Show Items"
        {{action 'Show_item' this.start}} /> 
</div> 
<br /><br /> 
<input type="button" id="push-item"
    value="Push More Items"
    {{action 'Push_item' }} /> 
  
  
{{outlet}}

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

Ember.js EmberArray objectAt 方法

參考:https://api.emberjs.com/ember/2.14/classes/Ember.Array/methods/objectAt?anchor=objectAt



相關用法


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