路由器显然在Backbone.js本地不执行任何操作 [英] Routers apparently doing nothing locally in Backbone.js

查看:65
本文介绍了路由器显然在Backbone.js本地不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在摆弄Backbone.js,除了路由器外,其他所有功能(模型/视图/事件/集合)都可以使用.这是我的js/router/test.js文件.

I recently was fiddling with Backbone.js and I got everything to work (Models/Views/Events/Collections) except the Router. Here is my js/router/test.js file.

var Router = Backbone.Router.extend({
    routes: {
        ''              :   'index',
        'projects/:id'  :   'show'
    },  

    start: function() {
        Backbone.history.start({ pushState : true }); 
    },  

    index: function() {
        alert('index');
    },  

    show: function(id) {
        alert(id);
    }   
});

var router = new Router();
router.start();

这是我的test.html文件.

And here is my test.html file.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title> sup </title>
    </head>
    <body>
        <p> hello </p>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!-- jQuery -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"> </script> <!-- underscore.js -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"> </script> <!-- backbone.js -->
    <script src="js/routers/test.js"></script>
    </body>
</html>

如果我在初始化中添加了警报(嗨!"):在路由器中,警报会正确弹出.我导航到file:///Users/myname/Projects/backbone/test.html作为基础页面.我应该在某个地方设置urlRoot吗?任何帮助表示赞赏.

If I put an alert('Hi!') in initialize: within my Router the alert pop ups correctly. I am navigating to file:///Users/myname/Projects/backbone/test.html as the base page. Should I be setting a urlRoot somewhere? Any help is appreciated.

推荐答案

问题是Backbone.js使用file:///URL的行为有所不同,并且需要由网络服务器来提供文件.

The problem was that Backbone.js acts differently using file:/// URLs and needs to have a webserver serving the files.

解决方案是将文件移到〜/Sites文件夹中,然后配置并启动Apache.我在这里遵循了指南: http ://www.coolestguidesontheplanet.com/downtown/get-apache-mysql-php-and-phpmyadmin-working-osx-109-mavericks .

The solution was to move my files into my ~/Sites folder and configure and start Apache. I followed the guide here: http://www.coolestguidesontheplanet.com/downtown/get-apache-mysql-php-and-phpmyadmin-working-osx-109-mavericks.

这篇关于路由器显然在Backbone.js本地不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆