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


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


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

reverseObjects()方法用於在數組中反轉對象。

用法:

reverseObjects( );

參數:這不需要任何爭論。

返回值:EmberArray接收器。

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

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route reverseObject1

應用程序/路線/reverseObject1.js

Javascript


import Route from '@ember/routing/route';
export default class PartyRoute extends Route {
    partyItems = [
        'Oxygen',
        'Tenet',
        'Source Code',
        'Infine',
        'Loopert',
        'Tenet',
        'SpiderHead',
        'The Thing',
        'A Quiet Place',
        'The Invisible Man',
        'Looper',
        'The Thing',
        'Ad Astra'
    ];
    temp = [
        'Shutter Island',
        'Matrix',
        'Avatar',
        'Fantastic Beast',
        'Avengers',
        'Fantastic four',
        'HulK',
        'Superman',
        'Spiderman',
        'The Batman'
    ];
    item;
    idx;
    model() {
        return this.partyItems;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('partyItems', this.partyItems);
        controller.set('item', this.item);
        controller.set('idx', this.idx);
        controller.set('temp', this.temp);
    }
}

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

Javascript


import Ember from 'ember';
import { reverseObjects, unshiftObject, unique, }
    from '@ember/array';
export default Ember.Controller.extend({
    actions: {
        invert() {
            this.partyItems.reverseObjects( );
             
        },
        insert_first(data) {
            this.partyItems.unshiftObject(data);
        },
        remove_d() {
            let res = this.partyItems.unique();
            this.partyItems.setObjects(res)
        },
    },
});

應用程序/模板/reverseObject1.hbs

Javascript


{{page-title "reverseObjecets"}}
<h3>Here is a list of items: </h3>
<ul>
    {{#each @model as |party|}}
        <li>{{party}}</li>
      {{/each}}
</ul>
<br /><br />
<div>
    <label>Enter Item: </label>
      {{input value=this.item}}
</div>
<div>
    <input
        type="button"
        id="insert-beginning"
        value="Insert at Beginning"
        {{action "insert_first" this.item}}
      />
</div>
<br />
<input
    type="button"
      id="reverse_list"
      value="Reverse List"
      {{action "invert"}}
/>
<br /><br />
<input
    type="button"
      id="remove-duplicates"
      value="Remove duplicates"
      {{action "remove_d"}}
/>
{{outlet}}

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

Ember.js ArrayProxy反向對象方法

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

ember generate route reverseObject2
  • 應用程序/路線/reverseObject2.js

Javascript


import Route from '@ember/routing/route';
export default class ListRoute extends Route {
    list = [
    "Digital Camera",
    "Speakers",
    "Balloons",
    "Scissors",
    "Cups",
    "Table Confetti",
    "Scissors",
    "Wine",
    "Napkins",
    "Party Plates",
    "Speakers",
    "Music System",
    "Cups",];
    item;
    idx;
    model() {
        return this.list;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('list', this.list);
        controller.set('item', this.item);
        controller.set('idx', this.idx);
    }
}
  • 應用程序/控製器/reverseObject2.js

Javascript


import Ember from 'ember';
import { reverseObjects, uniq, setObjects, unshiftObject }
    from '@ember/array';
export default Ember.Controller.extend({
    actions: {
        reverse() {
            this.list.setObjects(this.list.reverseObjects());
        },
        remove_d() {
            let k = this.list.uniq();    
            this.list.setObjects(k);
        },    
        add_first(item) {
            this.list.unshiftObject(item);
        },
    },
});
  • 應用程序/模板/reverseObject2.hbs

Javascript


{{page-title "reverseObjects"}}
<h3>Here is a list of Items: </h3>
<ul>
    {{#each @model as |i|}}
        <li>{{i}}</li>
      {{/each}}
</ul>
<br />
<div>
    <label>Enter Item: </label>
      {{input value=this.item}}
</div>
<div>
    <input
        type="button"
        id="insert-item"
        value="Insert at Beginning"
        {{action "add_first" this.item}}
      />
</div>
<br />
<input
    type="button"
      id="remove-duplicates"
      value="Remove duplicates"
      {{action "remove_d"}}
/>
<br /><br />
<input
    type="button"
      id="reverse_list"
      value="Reverse List"
      {{action "reverse"}}
/>
{{outlet}}

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

Ember.js ArrayProxy反向對象方法

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



相關用法


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