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


Embeer.js ObjectProxy get()用法及代碼示例


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

get() 方法用於從對象中檢索傳遞的屬性的值。

用法:

get( keyName );

參數:

  • keyName: 它是我們想要其值的屬性的名稱。

返回值:屬性值或未定義。

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route get1

應用程序/路線/get1.js

Javascript


import Route from '@ember/routing/route';
import EmberObject from '@ember/object';
import { } from '@ember/array';
export default class FruitsRoute extends Route {
    students = [
        EmberObject.create({ name: 'Rahul', 
            Class: 11, marks: 75 }),
        EmberObject.create({ name: 'Sam', 
            Class: 12, marks: 59 }),
        EmberObject.create({ name: 'David', 
            Class: 11, marks: 67 }),
        EmberObject.create({ name: 'Druv', 
            Class: 12, marks: 44 }),
        EmberObject.create({ name: 'Mahan', 
            Class: 12, marks: 87 }),
        EmberObject.create({ name: 'Ram', 
            Class: 11, marks: 71 }),
        EmberObject.create({ name: 'Dadu', 
            Class: 12, marks: 58 })
    ];
    model() {
        return this.students;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('students', this.students);
    }
}

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

Javascript


import Ember from "ember";
import { isAny, isEvery } from "@ember/array";
export default Ember.Controller.extend({
    actions: {
        increase(data) {
            this.students.forEach((item) => 
                item.incrementProperty('marks', 
                JSON.parse(data)));
        },
        decrease(data) {
            this.students.forEach((item) => 
                item.decrementProperty('marks', 
                JSON.parse(data)));
        },
        remove(item) {
            this.students.set('[]', this.students.without
                (this.students.findBy('name', item)));
        },
        show_first() {
            let ans = this.students.get('firstObject');
            alert(ans.name)
        },
        show_last() {
            let ans = this.students.get('lastObject');
            alert(ans.name)
        },
    },
});

應用程序/模板/get1.hbs

Javascript


{{page-title "get"}}
<h3>Students List: </h3>
<table>
    <tr>
        <th>Name</th>
        <th>Class</th>
            <th>Marks</th>
      </tr>
      {{#each @model as |detail|}}
        <tr>
            <td>{{detail.name}}</td>
              <td>{{detail.Class}}</td>
              <td>{{detail.marks}}</td>
        </tr>
      {{/each}}
</table>
<br />
<div>
    <label>Enter value: </label>
      {{input value=this.item}}
</div>
<input
    type="button"
      id="increase-marks"
      value="Increase All marks by Value"
      {{action "increase" this.item}}
/>
<br /><br />
<div>
    <label>Enter value: </label>
      {{input value=this.item2}}
</div>
<input
    type="button"
      id="decrease-marks"
      value="decrease All marks by Value"
      {{action "decrease" this.item2}}
/>
<br /><br />
<div>
    <label>Enter Name: </label>
      {{input value=this.item3}}
</div>
<input
    type="button"
      id="remove-student"
      value="Remove Student"
      {{action "remove" this.item3}}
/>
<br /><br />
<input
    type="button"
      id="first-student"
      value="Show First Student in list"
      {{action "show_first"}}
/>
<br /><br />
<input
    type="button"
      id="last-student"
      value="Show Last Student in list"
      {{action "show_last"}}
/>
{{outlet}}

輸出:訪問 localhost:4200/get1 查看輸出。

Ember.js 對象代理 get() 方法

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

ember generate route get2

應用程序/路線/get2.js

Javascript


import Route from '@ember/routing/route';
import EmberObject from '@ember/object';
export default class WebsitesRoute extends Route {
    food = [
        EmberObject.create({
            food: 'apple',
            isFruit: true,
            quant: 3,
        }),
        EmberObject.create({
            food: 'Potato',
            isFruit: false,
            quant: 4,
        }),
        EmberObject.create({
            food: 'Banana',
            isFruit: true,
            quant: 2,
        }),
        EmberObject.create({
            food: 'Burgur',
            isFruit: false,
            quant: 2,
        }),
        EmberObject.create({
            food: 'Orange',
            isFruit: true,
            quant: 7,
        }),
    ];
    temp;
    model() {
        return this.food;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('food', this.food);
        controller.set('temp', this.temp);
        controller.set('food2', this.food2);
    }
}

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

Javascript


import Ember from 'ember';
export default Ember.Controller.extend({
    value: 0,
    actions: {
        remove_item(item) {
            this.food.set('[]', this.food.without
            (this.food.findBy('food', item)));
        },
        list() {
            let ans = '';
            this.food.forEach((item) => ans += 
                item.get('food') + '\n');
            alert(ans)
        },
        increase() {
            this.food.forEach((item) => 
                item.incrementProperty('quant'));
        },
        decrease() {
            this.food.forEach((item) => 
                item.decrementProperty('quant'));
        },
    },
});

應用程序/模板/get2.hbs

Javascript


{{page-title "get"}}
<h3>List of Item in Buckets</h3>
<table>
    <tr>
        <th> Food_Name </th>
        <th>Bucket </th>
        <th>Fruit </th>
      </tr>
      {{#each @model as |website|}}
        <tr>
            <td>{{website.food}}</td>
              <td>{{website.quant}}</td>
              <td>{{website.isFruit}}</td>
        </tr>
      {{/each}}
</table>
<br />
<div>
    <label>Enter value: </label>
      {{input value=this.item}}
</div>
<input
    type="button"
      id="R-item"
      value="Remove this item"
      {{action "remove_item" this.item}}
/>
<br /><br />
<input 
    type="button"
    id="list-items"
    value="List All Items"
    {{action "list"}} />
<br /><br />
<input
    type="button"
      id="increase-property"
      value="Increase Quantity"
      {{action "increase"}}
/>
<br /><br />
<input
    type="button"
      id="decrease-property"
      value="decrease Quantity"
      {{action "decrease"}}
/>
{{outlet}}

輸出:訪問 localhost:4200/get2 查看輸出。

Ember.js 對象代理 get() 方法

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



相關用法


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