在Subscribe函数中从observableArray中删除元素而不再次发送信号
Remove element from observableArray within Subscribe function without signalling it again
我想做的是:
var queueManagerClass = function() {
this.queue = ko.observableArray();
this.queue.subscribe( function(theChangedQueue) {
// Do some nice things..
// Nice things are over, remove the last item from the queue..
this.queue.remove(theChangedQueue.pop());
}.bind(this));
};
除了两个问题:当我调用this.queue.remove(item);我就会陷入一个无限循环…订阅函数会一次又一次地调用self .
我知道有一个选项可以暂时"取消绑定"订阅函数,但我不能冒这样的风险,我错过了在取消绑定和再次绑定的平均时间插入的queueItem。
我希望你能理解我的(不太好的)英语。
谢谢你的时间!
解决这个问题的一种方法是使它可以检测到您正在删除项目,并在发生时特别忽略该事件。这可以通过使用本地存储"isremoting"状态来实现。例如
var isRemoving = false;
this.queue.subscribe( function(theChangedQueue) {
if (isRemoving) {
return;
}
// Do some nice things..
// Nice things are over, remove the last item from the queue..
isRemoving = true;
this.queue.remove(theChangedQueue.pop());
isRemoving = false;
}.bind(this));
相关文章:
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 不再使用innerHTML进行AJAX响应.(代码未得到响应)
- 不再需要时使用jQuery/kill实例
- 当我更改innerHtml时,引导选项卡事件不再工作
- C++信号回调(类似javascript)
- webkitspeechrecognition不再提示获得权限
- 即使被信号处理程序中断,node.js代码也会一直运行到完成吗
- 升级到Ember 1.0计算属性在视图中不再有效
- Meteor应用程序突然不再在iOS上运行
- 天气应用 API 不再工作
- AngularJS Dropdown在使用指令后不再工作
- 按钮点击不再触发
- ActiveX ListControl 事件在 Windows 更新后不再起作用
- 为什么动画完成后我的元素不再隐藏
- 如何在单击/接受/确认按钮后不再显示模态
- 为什么我的附加组件面板的内容脚本在更改 contentURL 后不再响应端口消息
- 是否不再需要 target=“_blank” 中的下划线
- 虚拟键盘在更新到 Chrome v50 后不再工作
- 下拉菜单转换为单选按钮,不再触发特定功能
- Firebug不再显示JS错误