1

我刚刚开始使用骨干网(对于模块使用 requirejs)。

在我看来,jQuery 可以做什么和 Backbone 可以做什么之间存在重叠。这让我想知道我应该使用哪个选项。

例如,我可以通过以下方式收听 even:

$('selector').click( function(event) {...

或者

var myView = Backbone.View.extend({
  events: {
    "click selector": "onClickSelector",
  }, ...

1)我应该使用上述哪个选项?

Jquery 支持我只是不知道如何使用 Backbone 管理的东西。比如JqueryUI的自动补全功能:

$('selector').autocomplete({ ...

2)我如何使用主干实现自动完成?我只是在视图呈现后在一个方法中完成所有这些onPostRender()吗?

我发现很多示例似乎都向 View 对象添加了新成员。例如:

var myView = Backbone.View.extend({
  events: {
    deletePressed: function() { ... },
    submitPressed: function() { ... },
    editPressed: function() { ... },
  }, ...

如果这些事件的功能很简单,我可以看出这是有道理的,但是,如果行为更加复杂,则 View 对象将变得非常复杂。

3)我应该在匿名函数中实现复杂的行为还是应该调用命名函数?

4)与上述类似,如果我有一个被许多事件(例如resetForm())使用的通用函数,是否应该将其声明为名称函数视图的成员?

我目前将我的 jquery 对象作为变量存储在模块中

var $mySelector = $(selector);

或者我可以将它们附加到视图

var myView = Backbone.View.extend({
  initialize: {
    this.mySelector = $(selector);
  }, ...

5)我应该让我的 JQuery 对象成为 View 的一部分还是 AMD 模块的一部分:

4

1 回答 1

1

以下是您的问题的答案:

1 : Go for the second option the one with events property in the view. Note : You can bind events using this approach to only those elements which are children of the element you specified as el in your view.

2: Yes you can do it in onPostRender().

3: If you want handleDeletePressed(), handleSubmitPressed() and handleEditPressed() to be reused or they have to do lot many things I suggest it is better to create named functions and specify them as :

events: {
    deletePressed: 'handleDeletePressed',
    submitPressed: 'handleSubmitPressed',
    editPressed: 'handleEditPressed',
  }

4: You can declare it as a member of view.

5: At any instance inside your view you can use this.$('selector'), to get jQuery object. If you want you can create jQuery objects as well.

于 2013-09-09T12:08:31.480 回答