我試圖用Backbone.js進行實驗,並嘗試重新配置其中一個標準ToDo教程。在路由器中初始化視圖
這是todo.js文件我開始用(不知道我來自哪裏得到它):
$(function(){
AppView = Backbone.View.extend({
el: $(".content"),
events: {
"keypress #new-todo": "createOnEnter",
},
createOnEnter: function(e) {
if (e.keyCode != 13) return;
$("#todo-list").append("<li>"+$("#new-todo").val()+"</li>")
$("#new-todo").val('');
}
});
App = new AppView;
});
我想看看,如果我能想出如何運行通過路由器來代替一切,所以我試過這個:
$(function(){
AppView = Backbone.View.extend({
el: $(".content"),
initialize: function(options) {
this.router = this.options.router;
},
events: {
"keypress #new-todo": "createOnEnter",
},
createOnEnter: function(e) {
if (e.keyCode != 13) return;
var term = $("$new-todo").val()
$("#todo-list").append("<li>"+term+"</li>")
$("#new-todo").val('');
this.router.navigate("stage/"+term);
},
});
// create router
AppRouter = Backbone.Router.extend({
initialize: function() {
// create view when router is initialized
new AppView({router : this});
},
routes: {
"stage/:stage" : "renderStage"
},
renderStage: function(stage) {
alert(stage);
}
});
App = new AppRouter;
});
但似乎沒有任何工作。沒有任何事件從視野中消失,也沒有任何路線起火。當我在控制檯的調試,我可以訪問App
它具有以下參數和方法:
_extractParameters
_routeToRegExp
bind
initialize
navigate
route
trigger
unbind
constructor
嘗試啓動歷史記錄:Backbone.history.start();在實例化路由器後 – rkw 2011-12-22 07:42:28