简体   繁体   English

多个路由在骨干.js中无法正常工作

[英]Multiple routes not working correctly in backbone.js

I'm trying to create a basic webapp that displays images when a specific URL is reached. 我正在尝试创建一个基本的Web应用程序,当到达特定的URL时显示图像。 In this case, I'm using backbone.js's hash system. 在这种情况下,我正在使用bone.js的哈希系统。

I'm trying to make it so that when "www.website.com/index.html#1" is reached, the first image is displayed using some JavaScript that I have. 我正在尝试使其达到“ www.website.com/index.html#1”时,使用我拥有的一些JavaScript显示第一张图像。 I also need it so that if "www.website.com/index.html#1/#3/#5" is reached, the first, third, and fifth image is displayed. 我还需要它,以便如果到达“ www.website.com/index.html#1/#3/#5”,则显示第一张,第三张和第五张图像。 I know that I have to use multiple routes to do this, but I'm not sure how. 我知道我必须使用多个路由来执行此操作,但是我不确定如何执行。

I have one working route for the first image that works awesomely. 对于第一张效果非常不错的图片,我有一条工作路线。 I just don't know how to adapt it so that it works with multiple routes. 我只是不知道如何适应它,以使其适用于多条路线。

Here's the working hash - 这是工作哈希-

<script>
    $(function(){

    var hideOne = function () {
        //alert("hideOne");
        var elem = document.getElementById("one");
        elem.className = "hide";
    };

    var Workspace = Backbone.Router.extend({
      routes: {
        "test":"test",// #test
      },    
      test: hideOne
    });

    var router = new Workspace();
    Backbone.history.start(); 

});
</script> 

It's awesome, it works, it doesn't even refresh the page. 很棒,可以用,甚至不刷新页面。 But when I try to add another route to that, it all fails. 但是,当我尝试添加另一条路由时,一切都失败了。 Like, if I added a "test1":"test1" under the "test":"test" , the original "test":"test" won't work anymore(neither will the new one, of course). 就像,如果我添加了一个"test1":"test1"下的"test":"test" ,原来的"test":"test"将不再有效(都不会对新的一个,当然)。

I've even tried copying+pasting that entire block of code and trying to make a whole new route block of code. 我什至尝试复制并粘贴整个代码块,并尝试制作一个全新的代码路由块。 That doesn't work either. 那也不行。 I'm really stumped here. 我真的很在这里

Any suggestions would be awesome. 任何建议都很棒。

Thanks 谢谢

You should limit the scope of your first use case. 您应该限制第一个用例的范围。 Don't depend on external functions for now. 现在不要依赖外部功能。 Do something like 做类似的事情

routes: {
        "test":function(){
            alert("test");
        },
        "test2":function(){
            alert("test2");
        }            
      },

Then change to 然后更改为

routes:  {
            "test":"test",
            "test2":"test2"
         },
         {
            test: function(){
              alert("test");
            },

            test2: function(){
              alert("test2");
            }
         }

Read more: http://mrbool.com/backbone-js-router/28001#ixzz3ANyS0hkR 了解更多: http : //mrbool.com/backbone-js-router/28001#ixzz3ANyS0hkR

Once you have that working, then start working on DOM manipulation. 工作完成后, 就可以开始进行DOM操作了。

routes: {
  "?*:queryString": 'showImages'
},

showImages: function(queryString) {
  console.log(queryString); // #1#3#5
}

You can use the route "?*:queryString" to match this URL "www.website.com/index.html#?#1#3#5". 您可以使用路由"?*:queryString"来匹配此URL“ www.website.com/index.html#?#1#3#5”。 The functions "showImages" will be called and passing the value #1#3#5 in the queryString param. 函数“ showImages”将被调用并在queryString参数中传递值#1#3#5

So from other questions posted on StackOverflow, and some that I posted myself, some great users have helped me out and I've solved it. 因此,从在StackOverflow上发布的其他问题以及我自己发布的一些问题中,一些出色的用户帮助了我,我已经解决了。

Here's the code that I have to use - 这是我必须使用的代码-

<script>
  $(function(){
    var hideMany = function () {
      var toHide = [].slice.call(arguments);
      toHide.forEach(function (id) {
        if(id === null) { return }
        var elem = document.getElementById(id);
        if(elem) {
          elem.className = "hide";
        }
      });
    };

    var Workspace = Backbone.Router.extend({
      routes: {
        "hide(/:a)(/:b)(/:c)" : "test"
      },
      test: hideMany
    });

    var router = new Workspace();
    Backbone.history.start();

  });
</script>

So when you type "www.website.com/index.html#hide/ID1/ID2/ID3", it'll hide the elements with the IDs that you typed in. 因此,当您键入“ www.website.com/index.html#hide/ID1/ID2/ID3”时,它将隐藏具有您键入的ID的元素。

I don't fully understand it, but I'm working on breaking it down and figuring out how it works. 我不完全了解它,但是我正在努力分解它并弄清楚它是如何工作的。 Thanks for all the help, guys! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM