我目前正在開始使用Backbone.js。我用Backbone寫了一些例子,它們工作正常。 但現在我需要使用Rails 3.1和CoffeeScript的Backbone.js。 我使用了我的工作良好的示例,並使用backbone-rails gem重寫了CoffeeScript。命名空間Backbone.js意見不發射事件
並得到以下問題。 我simplyfied代碼,但問題仍然剩餘
我已經得到了以下文件:
在這裏,我根據我在軌main_controller開始我的主幹應用程序在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,namespacing和backbone-rails gem的應用程序時,一切都很好。
Bytheway,appRouter正常工作,goalsView對象也被成功創建,但事件不會因爲某些原因而觸發。
請給我一些暗示,因爲我真的卡住了...
哇,謝謝!真的行! –