主干路由器不会触发事件

发布于 2024-12-15 16:51:21 字数 2498 浏览 1 评论 0原文

当应用程序启动时,我在 dom 上有一些导航链接,我希望通过 Backbone 劫持这些链接以进行渐进增强(主干应用程序底层有一个非常静态的文本内容版本)。

html 看起来像这样:

...
<body>
<header>
<nav>
<ol>
<li>
<a href="/en/home">home</a>
</li>
<li>
... few more links
</header>

然后我的应用程序使用以下方式实例化:

var App = (function(fw){
 var $      = fw;
 var workspace            = {};
 var self     = {};
 var lang     = "en";
 var models     = {...};
 var views     = {...};
 var collections   = {...};
 self.init = function() {
  workspace = new Workspace(
  {
    routes: {
     "/": "home",
     "/home": "home",
     "/terms": "terms",
     "/news": "blog"
   },
    lang : lang
  })
 }
 return self;
});
var app;
// launch
$(document).ready(function() {
 app = new App(jQuery);
 app.init();
});

工作区只是一个路由器,来自应用程序的那些路由通过初始化函数正确处理到路由器中,链接也可以工作并按预期更改 URL 并使用旧浏览器上的哈希值。问题是路由器/工作区本身没有进行任何回调。它只是静音,并且在单击时不会触发功能

这是我的工作区/路由器:

var Workspace = Backbone.Router.extend({

 routes : {},
 //functions                     <------------THESE
 home:    function(e){
  e.preventDefault();
  console.log("home here");
  App.views.HomeView.render();
 },
 terms:    function(){          //<-----------NEVER
  console.log("terms here");
  App.views.TermsView.render();
 },
 blog:    function(){           //<-----------FIRE
  console.log("blog here");
 },
 initialize:  function(params){
  var t = this;
  var tmpr = {};
  for(var i in params.routes)
  {
    //this just fuses lang and each route so /home becomes /en/home 
                  tmpr["/"+params.lang+i] = params.routes[i];
  }
  this.routes = tmpr; // this is fine and when logged it shows nicely with all routes looking good
  // router will only ever initialize once so lets deal with the stuff currently on the DOM before the app is inited.
  $("a",$("header")).click(function(e){
   e.preventDefault();
   Backbone.history.navigate($(this).attr("href"),true);//<-- true is set? should fire methods?
   // I've also tried all kinds of variations like t.navigate(..) where t is this workspace
  });
//this also seems to be fine and either does hashes or states
  if(history && history.pushState) {
   Backbone.history.start({
    pushState : true
   });
   console.log("has pushstate");
  } else {
   Backbone.history.start();
   console.log("no pushstate");
  }
  console.log("Router inited with routes:",this.routes);//logs routes nicely
 }
});

I have some navigation links on the dom when the app is started which I wish to hijack for progressive enhancement via Backbone (theres a very static text content version underlying the backbone app).

The html looks like this:

...
<body>
<header>
<nav>
<ol>
<li>
<a href="/en/home">home</a>
</li>
<li>
... few more links
</header>

Then my App is instantiated using:

var App = (function(fw){
 var $      = fw;
 var workspace            = {};
 var self     = {};
 var lang     = "en";
 var models     = {...};
 var views     = {...};
 var collections   = {...};
 self.init = function() {
  workspace = new Workspace(
  {
    routes: {
     "/": "home",
     "/home": "home",
     "/terms": "terms",
     "/news": "blog"
   },
    lang : lang
  })
 }
 return self;
});
var app;
// launch
$(document).ready(function() {
 app = new App(jQuery);
 app.init();
});

The workspace is just a router and those routes from the app are correctly processed into the router via the initialize function, also the links work and change the URL as expected and do it with hashes on older browsers. The problem is that no callbacks are ever made within the Router/Workspace itself. It's simply mute and doesnt fire the functions when the clicks are made

Here's my Workspace/Router:

var Workspace = Backbone.Router.extend({

 routes : {},
 //functions                     <------------THESE
 home:    function(e){
  e.preventDefault();
  console.log("home here");
  App.views.HomeView.render();
 },
 terms:    function(){          //<-----------NEVER
  console.log("terms here");
  App.views.TermsView.render();
 },
 blog:    function(){           //<-----------FIRE
  console.log("blog here");
 },
 initialize:  function(params){
  var t = this;
  var tmpr = {};
  for(var i in params.routes)
  {
    //this just fuses lang and each route so /home becomes /en/home 
                  tmpr["/"+params.lang+i] = params.routes[i];
  }
  this.routes = tmpr; // this is fine and when logged it shows nicely with all routes looking good
  // router will only ever initialize once so lets deal with the stuff currently on the DOM before the app is inited.
  $("a",$("header")).click(function(e){
   e.preventDefault();
   Backbone.history.navigate($(this).attr("href"),true);//<-- true is set? should fire methods?
   // I've also tried all kinds of variations like t.navigate(..) where t is this workspace
  });
//this also seems to be fine and either does hashes or states
  if(history && history.pushState) {
   Backbone.history.start({
    pushState : true
   });
   console.log("has pushstate");
  } else {
   Backbone.history.start();
   console.log("no pushstate");
  }
  console.log("Router inited with routes:",this.routes);//logs routes nicely
 }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妥活 2024-12-22 16:51:21

initialize 函数运行时,路由已经被绑定(参见 骨干来源)。

您可以使用 _bindRoutes 函数触发路由绑定:

this.routes = tmpr;
this._bindRoutes();

The routes have already been bound when the initialize function is run (see the backbone source).

You can trigger the routes binding with the _bindRoutes function:

this.routes = tmpr;
this._bindRoutes();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文