Backbone模板没有出现在Ruby on Rails的html.erb文件中

时间:2013-10-15 00:51:07

标签: html ruby-on-rails backbone.js underscore.js erb

我正在尝试将我的HTML文件移动到html.erb文件中,但是我遇到了问题,因为我使用的是Backbone.js。

我最初遇到了<%和<%=标签的问题,因为Ruby使用了它,但我更改了我的underscore.js文件

   _.templateSettings = {
    interpolate : /\{\{=(.+?)\}\}/g,
    escape : /\{\{-(.+?)\}\}/g,
    evaluate: /\{\{(.+?)\}\}/g
  };

而不是默认值。现在我不确定为什么我的内容没有出现在html.erb文件中。我收到错误:Backbone.history已经启动

这是我的html.erb文件:

<%= javascript_include_tag "Lessons.js"%>

   <script type="text/template" id="main-lesson-template">
        <!-- Note: Lesson (lesson name) -->
        <button class="btn btn-danger" style="width: 100%" id="Lesson {{= title }}">{{= title }}</button>
        {{ _.each(sublessons, function(sublesson) { }}
            <button class="btn btn-warning {{= title }}" style="opacity: 0.85; -moz-opacity: 0.85; -webkit-opacity: 0.85; list-style: none; width: 100%; display: none;">{{= sublesson.title }}</button>
        {{ }); }}
    </script>

    <script type="application/javascript">
        jQuery(function(){
            window.library.fetch();
        });
    </script>



    <script type="text/template" id="library-template">
        <div id="left-container" style="float: left; left: -170px">
            <ul class="lessons" style="width: 180px; padding-left: 0px;"></ul>
        </div>
        <script>
            $('#left-container').css('position', 'relative').mouseover(function() {
                $(this).stop(true).animate({'left': '0px'}, 1000);
            }).mouseout(function() {
                $(this).stop(true).animate({'left': '-170px'}, 1000);
            });
        </script>
    </script>


<%= javascript_include_tag "Lessons.js"%>

可能是我的javascript文件:

window.Lessons = Backbone.Collection.extend({
    model: Lesson,
    url: './lessons.json'
});

window.library = new Lessons();

$(document).ready(function() {

    window.LessonView = Backbone.View.extend({
        tagName: 'div',
        className: 'lesson',
        template:  _.template($('#main-lesson-template').html()),


    initialize: function() {
        _.bindAll(this,'render');
    },

    render: function() {
        var renderedContent = this.template(this.model.toJSON());
        $(this.el).html(renderedContent);
        return this;
    }
});

window.SubLessonView = Backbone.View.extend({
    tagName: 'div',
    className: 'sub-lesson',
    template: _.template($('#main-lesson-template').html()),

    initialize: function() {
        _bindAll(this, 'render');
    },

    render: function() {
        var renderedContent = this.template(this.model.toJSON());
        $(this.el).html(renderedContent);
        return this;        
    }
});

window.LibraryView = Backbone.View.extend({

    tagName: 'section',
    template: _.template($('#library-template').html()),

    events: {
        'click .btn.btn-danger': 'showSubLessons',
        'click .btn.btn-warning': 'showLessonContent'
    },

    initialize: function() {
        _.bindAll(this, 'render',
            'showSubLessons');
        this.collection.bind('reset', this.render);
    },

    render: function() {
        var $lessons
        collection = this.collection;
        $(this.el).html(this.template({}));
        $lessons = this.$('.lessons');
        this.collection.each(function(lesson) {
            var view = new LessonView({
                model: lesson,
                collection: collection
            });
            $lessons.append(view.render().el);

            //Add all content to the content container
            for(var i = 0; i < view.model.attributes.sublessons.length; ++i) {
                $('#content').append("<div id='" + view.model.attributes.sublessons[i].title + " Content' style='display: none'>" + view.model.attributes.sublessons[i].text + "</div>");
            }
        });

        var $container = $('#container');
        $container.append(this.el);
    },

我的路由器:

window.BackboneLessons = Backbone.Router.extend({
            routes: {
                '':'home'

        },

        initialize: function() {
            this.libraryView = new LibraryView({
                collection: window.library
            });
        },

        home: function(){
            this.libraryView.render();
        }
    });

    window.App = new BackboneLessons();
    Backbone.history.start(); //offending line
});

1 个答案:

答案 0 :(得分:0)

我已经将我的javascript文件链接了两次,这就是Backbone.history.start被调用两次的原因。