我想制作一个类似队列的数组,而且我不知道如何移动到下一个元素。我没有使用for循环进行迭代,因为如果删除数组的元素,则会出现问题。
我能做什么?
编辑:我应该放一个示例代码:在控制器中创建数组:
listOfItems:[{name:'Jack',specialty:'engineer'},{name:"Eva",speciality:"doctor"}]
接下来将值设置到车把
counter:0,
actions:{
openModal()
{
this.set('openModal',true);
this.set('nameOfPerson',this.get('listOfItems.'+counter+'.name');
}
nextValue()
{
this.incrementProperty('counter'); //I want to implement there
//something like next Object
}
previousValue()
{
this.decrementProperty('counter'); // previous object there
}
acceptValues()
{
//don't know how to do a delete object there
}
}
我建议您为此使用一个新属性,如下所示。
counter: 0,
currentItem: function() {
return this.get('listOfItems').objectAt(this.get('counter'));
}.property('counter', 'listOfItems.[]')
actions:{
openModal() {
this.set('openModal',true);
this.set('nameOfPerson',this.get('currentItem.name');
},
acceptValues() {
let counter = this.get('counter');
this.get('listOfItems').removeAt(counter); // Assumes an Ember.MutableArray
if(counter > 0) {
// If we are at the first item, just let the second value be selected,
// if any other value is removed, decrement the counter by one to select
// the previous item
this.decrementProperty('counter');
}
}
}
这样,您也可以currentItem
根据需要直接绑定到模板中,因此nameOfPerson
可以避免currentItem.name
使用,而是使用它。
该acceptValues
方法只是从数组中删除项目,并且可以递减counter
。该currentItem
属性将取决于它listOfItems.[]
(例如,当项目数更改时更新),因此将无效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句