ember.js - Ember.run.debounce 不反跳

标签 ember.js typeahead.js

我在 Ember 的子组件中使用 Twitter Typeahead.js,我提供了一个 dataSource 函数(见下文)。 此数据源函数查询远程服务器。我想在 Ember 中消除这个查询,但它似乎不起作用。

这和runloop有关系吗?我应该包什么东西吗?

import Ember from 'ember';

export default Ember.Component.extend({

    dataResponse: [],
    dataSource: function () {
        var component = this;

        // function given to typeahead.js
        return function (query, cb) {

            var requestFunc = function () {
                var encQuery = encodeURIComponent(query);
                Ember.$.getJSON('/api/autocompletion?prefix=' + encQuery).then(function (result) {
                    // save results
                    component.set('dataResponse', result.autocompletion);
                    // map results
                    var mappedResult = Ember.$.map(result.autocompletion, function (item) {
                        return { value: item };
                    });
                    cb(mappedResult);
                });
            };

            // this is not debounced, why? :|
            Ember.run.debounce(this, requestFunc, 500); // debounce by 500ms  
        };
    }.property()

});

注意:我不将 Bloodhound 与 Typeahead.js 一起使用,因为我需要访问结果。定制解决方案一开始似乎更容易。

最佳答案

Debounce 的工作原理是根据上下文/函数创建唯一的键。当您随后调用它时,它会将现有键与传入的上下文/功能键进行比较。每次调用 debounce 时都会传入不同的函数,这就是为什么它无法按您期望的方式工作的原因。

关于ember.js - Ember.run.debounce 不反跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27124695/

相关文章:

javascript - 如何重置 typeahead.js 中使用的 Hogan.js 模板

ember.js - 在事件的 {{linkTo}} 周围创建一个 <li class ="active">

javascript - 在前端缓存动态生成的图像

javascript - 在 Ember 1.13.0 中获取 ('model' .addObjects() 时出错

javascript - Ember : Testing WillDestroyElement Logic of Component in an Integration Test

javascript - EMBER直接路由URL访问不加载数据

javascript - Typeahead.js 建议未被过滤

javascript - 与 ember js 一起使用时如何为 typeahead 指定预编译模板?

jquery - typeahead.js 根本没有响应 - Rails

javascript - 如何列出 Typeahead.js 和/或 Bloodhound 引擎的对象?