Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Namespaced Backbone.js视图不触发事件_Javascript_Ruby On Rails 3.1_Backbone.js_Coffeescript - Fatal编程技术网

Javascript Namespaced Backbone.js视图不触发事件

Javascript Namespaced Backbone.js视图不触发事件,javascript,ruby-on-rails-3.1,backbone.js,coffeescript,Javascript,Ruby On Rails 3.1,Backbone.js,Coffeescript,我目前正在开始使用Backbone.js。我已经用主干写了一些例子,它们工作得很好。 但是现在我需要在Rails 3.1和CoffeeScript中使用Backbone.js。 我拿了一些很好的例子,用主干rails gem重写了CoffeeScript 得到了以下问题。 我简化了代码,但问题仍然存在 我有以下文件: 在这里,我将根据rails应用程序中的主控制器在main.js.coffee文件中启动我的主干应用程序: $ = jQuery $-> CsfTaskManager.in

我目前正在开始使用Backbone.js。我已经用主干写了一些例子,它们工作得很好。 但是现在我需要在Rails 3.1和CoffeeScript中使用Backbone.js。 我拿了一些很好的例子,用主干rails gem重写了CoffeeScript

得到了以下问题。 我简化了代码,但问题仍然存在

我有以下文件:

在这里,我将根据rails应用程序中的主控制器在main.js.coffee文件中启动我的主干应用程序:

$ = jQuery
$->
  CsfTaskManager.init()
以下是主干应用程序说明:

#= require_self
#= require_tree ./templates
#= require_tree ./models
#= require_tree ./views
#= require_tree ./routers

window.CsfTaskManager =
  Models: {}
  Collections: {}
  Routers: {}
  Views: {}
  init: ->
    new CsfTaskManager.Routers.AppRouter()
    Backbone.history.start()
这是我的应用程序路由器:

class CsfTaskManager.Routers.AppRouter extends Backbone.Router
  initialize: (options) ->
    goalsBlock = new CsfTaskManager.Views.goalsView()

  routes:
    "!/": "root",
    some other routes...
最后,我认为:

class CsfTaskManager.Views.goalsView extends Backbone.View

  initialize: ->
    this.goals = new CsfTaskManager.Collections.GoalsCollection()

  el: $('div#app'),

  events:
    "click .add-btn": "addGoal"

  addGoal: ->
    alert('ji')
HTML页面包含以下代码:

<div id="app">
    <div class="app-screen hidden" id="goal-form" style="display: block; ">
      <button class="btn" id="load">
        Load
      </button>
      <h3>
        New Goal
      </h3>
      <div class="form-stacked">
        <form accept-charset="UTF-8" action="/goals" class="new_goal" id="new_goal" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="Pnt+V/tS1/b079M/1ZIRdw2ss1D6bvJKVh868DXRjUg="></div>
          <label for="goal_title">Title</label>
          <p></p>
          <input class="goal-title" id="goal_title" name="goal[title]" size="30" type="text">
          <p></p>
          <label for="goal_note">Note</label>
          <p></p>
          <input class="goal-note" id="goal_note" name="goal[note]" size="30" type="text">
        </form>
      </div>
      <p>
        <button class="add-btn btn">
          Add
        </button>
      </p>
      <ul id="goals-list"></ul>
    </div>
    <table class="app-screen bordered-table" id="calendar-grid" style="display: none; ">
      <tbody><tr>
        <td colspan="2">
          week
        </td>
      </tr>
      <tr>
        <td>
          day
        </td>
        <td>
          <div id="calendar"></div>
        </td>
      </tr>
    </tbody></table>
    <div class="app-screen hidden" id="role-form" style="display: none; ">
      <h3>
        New User Role
      </h3>
      <div class="form-stacked">
        <form accept-charset="UTF-8" action="/roles" class="new_role" id="new_role" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="Pnt+V/tS1/b079M/1ZIRdw2ss1D6bvJKVh868DXRjUg="></div>
          <label for="role_title">Title</label>
          <p></p>
          <input class="role-title" id="role_name" name="role[name]" size="30" type="text">
          <p></p>
          <label for="role_note">Note</label>
          <p></p>
          <input class="role-note" id="role_description" name="role[description]" size="30" type="text">
        </form>
      </div>
      <p>
        <button class="add-btn btn">
          Add
        </button>
      </p>
    </div>
  </div>

负载
新目标
标题

笔记

添加

    周 白天 新用户角色 标题

    笔记

    添加

    添加btn元素嵌套在#app中,但单击此按钮不会触发事件。 哪里会有麻烦

    以前,当我在一个.js文件中使用相同的应用程序时,没有coffeescript、名称空间和主干rails gem,一切都正常

    顺便说一句,appRouter工作正常,goalsView对象也成功创建,但由于某些原因事件不会触发


    请给我一些提示,因为我真的被卡住了…

    我想问题可能是
    CsfTaskManager
    中的以下行:

      el: $('div#app'),
    
    通常el应该是jQuery选择器字符串或DOM元素。这里传递的是一个jQuery对象。尝试将其更改为:

     el: 'div#app'
    
    (CoffeeScript中也不需要逗号)