Ember.js 是一个开源 JavaScript 框架,用于开发基于 Model-View-Controller (MVC) 架构的大型客户端 Web 应用程序。 Ember.js是使用最广泛的前端应用框架之一。它的目的是加速开发并提高生产力。目前,它被大量网站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。
invoke() 方法用于在接收器中实现该方法的每个对象上调用指定方法。
用法:
invoke( methodName, args );
参数:
- methodName:这是我们要调用的方法的名称。
- args: 它是一个可选参数,将传递该参数来调用该方法。
返回值:调用invoke的返回值。
要运行以下示例,您需要有一个 ember 项目。要创建一个,您需要先安装ember-cli。在终端中写入以下代码:
npm install ember-cli
现在您可以通过输入以下代码来创建项目:
ember new <project-name> --lang en
要启动服务器,请键入:
ember server
示例 1:键入以下代码以生成本示例的路由:
ember generate route invoke1
应用程序/路由/invoke1.js
import Route from '@ember/routing/route';
class employee {
name = null;
city = null;
gender = null;
zipCode = null;
constructor(name, city, gender, zipCode) {
this.name = name;
this.city = city;
this.gender = gender;
this.zipCode = zipCode;
}
show_details() {
return `${this.name} is Employee from
${this.city} zipCode ${this.zipCode}`;
}
}
export default class DetailsRoute extends Route {
details = [
new employee('Anubhav', 'Patna', 'M', '800020'),
new employee('Satyam', 'Delhi', 'M', '110012'),
new employee('Shivam', 'Patna', 'M', '530068'),
new employee('Ayushi', 'Jaipur', 'F', '302001'),
];
attr = ['name', 'city', 'gender', 'zipCode'];
temp;
start;
end;
model() {
return this.details;
}
setupController(controller, model) {
super.setupController(controller, model);
controller.set('details', this.details);
controller.set('attr', this.attr);
controller.set('temp', this.temp);
controller.set('temp2', this.temp2);
controller.set('end', this.end);
}
}
应用程序/控制器/invoke1.js
import Ember from "ember";
import { any, slice, reverseObjects, rejectBy }
from "@ember/array";
export default Ember.Controller.extend({
actions: {
checkCity(city) {
let res = this.details.any((person) =>
person.city == city);
alert(`Person from ${city} is ${res ? "" :
'not'} Present`);
},
getFemale() {
let tempItems = this.details.find((i)
=> i.gender == "F");
alert(tempItems.name);
},
show_Data() {
let ans = this.details.invoke('show_details');
alert(ans.join('\n'))
},
},
});
应用程序/模板/invoke1.hbs
{{page-title "invoke"}}
<h3>List of People: </h3>
<br /><br />
<table>
<tr>
<th>Name</th>
<th>Gender</th>
<th>City</th>
<th>Zip Code</th>
</tr>
{{#each @model as |detail|}}
<tr>
<td>{{detail.name}}</td>
<td>{{detail.gender}}</td>
<td>{{detail.city}}</td>
<td>{{detail.zipCode}}</td>
</tr>
{{/each}}
</table>
<br /><br />
<div>
<label>Enter City: </label>
{{input value=this.city}}
</div>
<div>
<input
type="button"
id="check-city"
value="Check"
{{action "checkCity" this.city}}
/>
</div>
<br />
<input
type="button"
id="get-female"
value="Get Female"
{{action "getFemale"}}
/>
<br /><br />
<div>
<input
type="button"
id="show-details"
value="Employee's details"
{{action "show_Data"}}
/>
</div>
{{outlet}}
- Output: 访问 localhost:4200/invoke1 查看输出
Ember.js ArrayProxy 调用方法
示例 2:键入以下代码以生成本示例的路由:
ember generate route invoke2
应用程序/路由/invoke2.js
import Route from '@ember/routing/route';
class fruit {
name = null;
isFruit = null;
color = null;
constructor(name, isFruit, color) {
this.name = name;
this.isFruit = isFruit;
this.color = color;
}
Show_item() {
return `${this.name} is ${this.color} color`;
}
}
export default class StudentsRoute extends Route {
fruits = [new fruit('Orange', true, 'orange',),
new fruit('Apple', true, 'red'),
new fruit('Pineapple', true, 'Green-grey'),
new fruit('Banana', true, 'yellow'),
new fruit('Potato', false, 'brown'),
new fruit('Cabbage', false, 'green'),
new fruit('Cauli-flower', false, 'green'),
];
temp2;
temp;
model() {
return this.fruits;
}
setupController(controller, model) {
super.setupController(controller, model);
controller.set('fruits', this.fruits);
controller.set('temp', this.temp);
controller.set('temp1', this.temp1);
controller.set('temp2', this.temp2);
}
}
应用程序/控制器/invoke2.js
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
find_Data() {
let ans = this.fruits.invoke('Show_item');
alert(ans.join('\n'))
},
insertItem(data, data1, data2) {
let temp = {
name: data,
isFruit: data1,
color: data2,
}
let ans = this.fruits.pushObject(temp);
},
},
});
应用程序/模板/invoke2.hbs
{{page-title "Student"}}
<h3>List of Students: </h3>
<br />
<table>
<tr>
<th>Name</th>
<th>isFruit </th>
<th>Color </th>
</tr>
{{#each @model as |detail|}}
<tr>
<td>{{detail.name}}</td>
<td> {{detail.isFruit}}</td>
<td>{{detail.color}}</td>
</tr>
{{/each}}
</table>
<br />
<br />
<div>
<input
type="button"
id="check-fruit"
value="Show Items details"
{{action "find_Data"}}
/>
</div><br /><br />
<div>
<label>Enter Fruit Name: </label>
{{input value=this.item1}}
</div><br />
<div>
<label>Item is Fruit: </label>
{{input value=this.item2}}
</div><br />
<div>
<label>Enter Fruit color: </label>
{{input value=this.item3}}
</div><br />
<div>
<input
type="button"
id="insert-item"
value="Insert Fruit"
{{action "insertItem" this.item1
this.item2 this.item3}}
/>
</div>
{{outlet}}
- Output: 访问 localhost:4200/invoke2 查看输出
Ember.js ArrayProxy 调用方法
参考:https://api.emberjs.com/ember/4.6/classes/ArrayProxy/methods/invoke?anchor=invoke
相关用法
- Embeer.js ArrayProxy insertAt()用法及代码示例
- Embeer.js ArrayProxy init()用法及代码示例
- Embeer.js ArrayProxy incrementProperty()用法及代码示例
- Embeer.js ArrayProxy includes()用法及代码示例
- Embeer.js ArrayProxy indexOf()用法及代码示例
- Embeer.js ArrayProxy isDestroyed用法及代码示例
- Embeer.js ArrayProxy isEvery()用法及代码示例
- Embeer.js ArrayProxy isAny()用法及代码示例
- Embeer.js ArrayProxy content用法及代码示例
- Embeer.js ArrayProxy cacheFor()用法及代码示例
- Embeer.js ArrayProxy objectAtContent()用法及代码示例
- Embeer.js ArrayProxy toString()用法及代码示例
- Embeer.js ArrayProxy replaceContent()用法及代码示例
- Embeer.js ArrayProxy removeObserver()用法及代码示例
- Embeer.js ArrayProxy destroy()用法及代码示例
- Embeer.js ArrayProxy uniqBy()用法及代码示例
- Embeer.js ArrayProxy reduce()用法及代码示例
- Embeer.js ArrayProxy compact()用法及代码示例
- Embeer.js ArrayProxy set()用法及代码示例
- Embeer.js ArrayProxy filter()用法及代码示例
- Embeer.js ArrayProxy unshiftObject()用法及代码示例
- Embeer.js ArrayProxy objectAt()用法及代码示例
- Embeer.js ArrayProxy addObject()用法及代码示例
- Embeer.js ArrayProxy getProperties()用法及代码示例
- Embeer.js ArrayProxy rejectBy()用法及代码示例
注:本文由纯净天空筛选整理自satyam00so大神的英文原创作品 Ember.js ArrayProxy invoke() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。