【问题标题】:Uncaught TypeError: Cannot call method 'navigate' of undefined backbonejs未捕获的类型错误:无法调用未定义骨干网的方法“导航”
【发布时间】:2013-12-24 07:55:54
【问题描述】:

我正在尝试使用 Eclipse 中的主干创建一个简单的页面导航程序。我的页面是......当我点击一个按钮时,它会转到一个特定的页面(结果)。但它会引发错误

未捕获的 TypeError:无法调用未定义的主干 min.js 的方法“导航”:

<html>
<head>
    <title>Button Click</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://twitter.github.com/hogan.js/builds/2.0.0/hogan-2.0.0.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js" type="text/javascript"></scr
    <script src="http://twitter.github.com/hogan.js/builds/2.0.0/hogan-2.0.0.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
</head>
<body>
    <script type="text/mustache" id="scriptemplate">
        <input type="submit" class = "btn" value="Search">
    </script>    
    <div id="search_container"></div>
    <script type="text/javascript">
        buttonview = Backbone.View.extend({
            initialize: function(){
                this.render();
            },
            render: function(){
                var template = _.template( $("#scriptemplate").html(), {} );
                // Load the compiled HTML into the Backbone "el"
                this.$el.html( template );
            },
            events:{   
                "click input[type=submit]":"doSearch"
            },
            doSearch: function() {
                console.log('click event reached');
                var MyApp = new Backbone.Router();
                MyApp.navigate('/result', {trigger: true});
            }
        });
        var search_view = new buttonview({ el: $("#search_container") });
    </script>
</body>
</html>

【问题讨论】:

    标签: javascript jquery backbone.js


    【解决方案1】:

    有两件事要做:

    1. 更改您的 Backbone 版本。

      http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js

    2. 在此处添加此代码。

      var MyApp = new Backbone.Router();

      Backbone.history.start();

      MyApp.navigate('/result', {trigger: true});

    在文档中查看:

    http://backbonejs.org/#Router

    【讨论】:

    • 感谢您的回复,但现在问题有所不同...我按照您所说的做了..它没有显示错误但没有所需的输出..我尝试在点击事件中从一个页面导航到另一个页面.在这里,当我单击它激活的按钮时,但没有导航到“/result 页面”,但 url 显示如下“localhost:8080/SinglePageProject/#result”.. 根本没有导航..
    • 是的,navigate 就是这样工作的。 Backbonejs是用来做SPA(单页应用)的,也就是说如果你想导航到另一个url,你的页面会刷新,所有的Backbone Object都会丢失,因为都是js对象。
    • 那么我如何导航到另一个页面??
    • @Shiva 可以直接使用js代码:window.loaction.href = 'Your url here';
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 2011-11-12
    • 2013-09-14
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多