无法获得骨干来渲染任何东西

时间:2015-08-10 14:05:55

标签: backbone.js

我试图接受一些backbone.js,但在第一道障碍时失败了。它只是不会渲染任何东西。我已经尝试了许多不同的“入门”和#39;文章,但仍然没有。没有错误,只是没有。我已经把它简化为我认为应该根据我所阅读的内容工作的东西,但现在我完全不确定任何事情。请帮忙......

编辑:使用JSfiddle更新:https://jsfiddle.net/zsLjawy0/

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script src="http://underscorejs.org/underscore-min.js"></script>
        <script src="http://backbonejs.org/backbone-min.js"></script>
    </head>

    <body>
        <script type="text/template" id="test">
            test
        </script>

        <script src="app.js"></script>
    </body>
</html>

app.js

$(document).ready(function(){

    TestView = Backbone.View.extend({
        initialize: function() {
            this.render();
            console.log('this does nothing');
        },
        render: function() {
            var template = _.template( $("#test").html(), {} );
            this.$el.html( template );

            return this; // Also does nothing
        }
    });  
});

2 个答案:

答案 0 :(得分:0)

正如我所看到的,您的应用程序只定义了TestView,但没有创建它的实例而且不呈现它。您应该创建一个TestView实例,渲染它并将渲染的内容添加到文档中。

答案 1 :(得分:0)

您的代码存在一些问题。

首先,正如Voice所示,在某些时候你需要创建一个视图实例,否则你的初始化渲染函数就无法发生。 ==&GT; MyView = new TestView();

第二点你需要定义一个模板和一个#el,这样你才能正确渲染。

TestView = Backbone.View.extend({
    el: #test,
    template: _.template(LoadAtemplateWithREQUIREJS_it_would_be_wise),

    initialize: function() {
        this.render();
        console.log('this does nothing');
    },
    render: function() {
        var template = 
        this.$el.html( this.template(someParams_amodelforinstance);

        return this; // Also does nothing
    }
}); 

最后一点,不要使用脚本作为div目标,使用div。 这个:

<div id="test"> </div>

我让你找出原因。

希望它有所帮助!

相关问题