最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbonejs underscore throttled function is not firing - Stack Overflow

programmeradmin2浏览0评论

I want to filter a backbone collection. Hence, i want to throttle the keyup event and fire when user is done typing or take a pause.

My before throttle function is firing and i am getting the log('before throttle'). However, actual filter filterByTitle is not firing. Any suggestion?

linkApp.Views.FilteredLinks = Backbone.View.extend({

    el:'#divFilter',

    events:{
        'keyup #filterTitle': "filterByTitleThrottled"
    },

    initialize:function(){          
    },

    render:function(){
    },      

    filterByTitleThrottled:function(){
        console.log('before throttle');
        _.throttle(this.filterByTitle, 100);
    },
    filterByTitle:function(){
        console.log('actual filter by title');
    }
});

I want to filter a backbone collection. Hence, i want to throttle the keyup event and fire when user is done typing or take a pause.

My before throttle function is firing and i am getting the log('before throttle'). However, actual filter filterByTitle is not firing. Any suggestion?

linkApp.Views.FilteredLinks = Backbone.View.extend({

    el:'#divFilter',

    events:{
        'keyup #filterTitle': "filterByTitleThrottled"
    },

    initialize:function(){          
    },

    render:function(){
    },      

    filterByTitleThrottled:function(){
        console.log('before throttle');
        _.throttle(this.filterByTitle, 100);
    },
    filterByTitle:function(){
        console.log('actual filter by title');
    }
});
Share Improve this question asked Nov 10, 2013 at 21:35 Jhankar MahbubJhankar Mahbub 9,84810 gold badges50 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

I think it would be better to _.throttle this.filterByTitle on initialize to make it works properly.

initialize:function(){
      this.filterByTitle = _.throttle(this.filterByTitle, 100);
},

You'll throttle it once and you'll get result that you're expecting.

When you call to _.throttle - it's creates and returns a new version of the passed function. And after this you can use her:

filterByTitleThrottled:function(){
    console.log('before throttle');
    var trottle = _.throttle(this.filterByTitle, 100);
    trottle();
}
发布评论

评论列表(0)

  1. 暂无评论