当前位置: 动力学知识库 > 问答 > 编程问答 >

backbone.js - events or functions for jquery in backbone

问题描述:

I'm new to learning backbone, and in a lot of examples and answers to questions here on SO, I see a lot of people just adding regular jQuery or javascript into the render functions rather than following the backbone way of

events: {

'draggable #drag_this': 'drag'

},

drag: function(){

// your jQuery draggable code

}

I'm hoping somebody can expand on the difference between these two ways of doing things, and why one would be preferred over another, and in which circumstances.

网友答案:

The events hash uses delegation automatically, which is a solid technique for creating less event listeners.

Essentially the two situations do the same thing, but registering listeners in the render() function is a manual way of doing it and the events hash is a declarative way of doing it.

If you go with the events hash you can call this.delegateEvents() and it will use the events hash you define by default. (This is good for if you remove the view from the dom and reattach it)

Personally I've using manual event binding in the render() function when I'm using a library that binds it's own listeners.(Think twipsy from twitter's bootstrap).

The Events hash is the way to go if it works for your app. It is definitely a 'more Backbone' way of handling events.

网友答案:

My understanding is that manually added events are not removed when the View is destroyed, potentially leading to "Zombie Views" and memory leaks if you're creating and destroying the View a lot - unless you also remove them before you destroy the view. E.g. $('.slider').slider("destroy").

分享给朋友:
您可能感兴趣的文章:
随机阅读: