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


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


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

decrementProperty() 方法用於將屬性的值設置為當前值減去某個值。

用法:

decrementProperty( keyName, decrement );

參數:

  • keyName: 它是我們想要減少其值的屬性的名稱。
  • decrement: 這是屬性將減少的數量。

返回值:新的屬性值。

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

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route decrement1

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


import Ember from 'ember'; 
import { decrementProperty } 
    from '@ember/array'; 
  
export default Ember.Controller.extend({ 
    year: 2022, 
    month: 9, 
    date: 15, 
    actions: { 
        olderD() { 
            this.incrementProperty('date'); 
        }, 
        youngerD() { 
            this.decrementProperty('date'); 
        }, 
        olderY() { 
            this.incrementProperty('year'); 
        }, 
        youngerY() { 
            this.decrementProperty('year'); 
        }, 
        olderM() { 
            this.incrementProperty('month'); 
        }, 
        youngerM() { 
            this.decrementProperty('month'); 
        }, 
    }, 
});

應用程序/模板/decrement1.hbs


{{page-title "DecrementProperty"}} 
<h1>Year {{this.year}}</h1> 
<input type="button" id="increase"
    value="+" {{action 'olderY'}}/> 
<input type="button" id="decrease"
    value="-" {{action 'youngerY'}}/> 
<hr/> 
<h1>Month {{this.month}}</h1> 
<input type="button" id="increase"
    value="+" {{action 'olderM'}}/> 
<input type="button" id="decrease"
    value="-" {{action 'youngerM'}}/> 
<hr/> 
<h1>Date {{this.date}}</h1> 
<input type="button" id="increaseD"
    value="+" {{action 'older'}}/> 
<input type="button" id="decrease"
    value="-" {{action 'youngerD'}}/> 
{{outlet}}

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

Ember.js ArrayProxy decrementProperty 方法

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

ember generate route decrement2

應用程序/路線/decrement2.js


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

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


import Ember from 'ember'; 
  
export default Ember.Controller.extend({ 
    value: 0, 
    actions: { 
        older() { 
            this.incrementProperty('value'); 
        }, 
        younger() { 
            this.decrementProperty('value'); 
        }, 
  
        addItem(data, data1, data2) { 
            let temp = {  
                food: data,  
                quant: data1,  
                isFruit: data2  
            } 
            this.food.addObject(temp) 
        }, 
    }, 
});

應用程序/模板/decrement2.hbs


{{page-title "decrementProperty"}} 
<h3>List of Item in Buckets</h3> 
  
<table> 
    <tr> 
        <th> Food Name   </th> 
        <th>Bucket  </th> 
        <th>Fruit  </th> 
    </tr> 
    {{#each @model as |i|}} 
    <tr> 
        <td>{{i.food}}</td> 
        <td>{{i.quant}}</td> 
        <td>{{i.isFruit}}</td> 
    </tr> 
    {{/each}} 
</table> 
<br /><br /> 
  
<div> 
    <label>Enter Item Name: </label> 
    {{input value=this.temp2}} 
</div> 
<br /> 
  
<div> 
    <label>Enter Quantity in Kg: </label> 
    {{input value=this.value}} 
</div> 
<input type="button" id="increase"
    value="+" {{action 'older'}}/> 
<input type="button" id="decrease"
    value="-" {{action 'younger'}}/> 
<br/> 
<br /> 
  
<div> 
    <label>Item is fruit of not : </label> 
    {{input value=this.temp}} 
</div> 
<br /><br /> 
<input type="button" id="all-Fruits"
    value="Add item"
    {{action 'addItem' this.temp2 this.value this.temp}} /> 
{{outlet}}

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

Ember.js ArrayProxy decrementProperty 方法

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



相關用法


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