当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Embeer.js Ember.NativeArray incrementProperty()用法及代码示例


Ember.js 是一个开源 JavaScript 框架,用于开发基于 Model-View-Controller (MVC) 架构的大型客户端 Web 应用程序。 Ember.js是使用最广泛的前端应用框架之一。它的目的是加速开发并提高生产力。目前,它被大量网站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

incrementProperty() 方法用于将属性设置为当前值加上一些值。

用法:

incrementProperty( keyName, increment );

参数:

  • keyName: 这是我们想要增加其值的属性的名称。
  • increment: 这是要增加的量。

返回值:新的属性值。

安装和运行 Ember.js 的步骤:

第 1 步:要运行以下示例,您需要有一个 ember 项目。要创建一个,您需要先安装ember-cli。在终端中写入以下代码:

npm install ember-cli

第 2 步:现在您可以通过输入以下代码来创建项目:

ember new <project-name> --lang en

要启动服务器,请键入:

ember serve

示例 1:键入以下代码以生成本示例的路由:

ember generate route increment1

应用程序/控制器/increment1.js

Javascript


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');
        },
    },
});

应用程序/模板/increment1.hbs

Javascript


{{page-title "incrementProperty"}}
<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/increment1 查看输出

Ember.js Ember.NativeArray增量属性方法

示例 2:键入以下代码以生成本示例的路由:

ember generate route increment2

应用程序/路线/increment2.js

Javascript


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);
    }
}

应用程序/控制器/increment2.js

Javascript


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)
        },
    },
});

应用程序/模板/increment2.hbs

Javascript


{{page-title "increaseProperty"}}
<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 /><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 or 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/increment2 查看输出。

Ember.js Ember.NativeArray增量属性方法

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



相关用法


注:本文由纯净天空筛选整理自jeemains0neet大神的英文原创作品 Ember.js Ember.NativeArray incrementProperty() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。