Backbone.js 中未定义向视图模板传递数据?

Passing data to view template is undefined in Backbone.js?

全部,

我目前正在尝试传递一个简单的 JSON 对象(尝试使用模型但仍然是同样的问题)。

我已经使用一个名为 "skills" 的变量初始化了我的视图,我想将该变量传递到我的模板中。但是在模板内部它说它是未定义的。

    define([
    'jquery',
    'underscore',
    'backbone',
    'text!templates/skillsPopup.html', //for templates you should prepend the path with !text
    'common'
], function ($, _, Backbone, SkillsTemplate, Common, Skills)
{
    'use strict';

    var FilterView = Backbone.View.extend({

        el: $("#skillsPopup"), //set this to the element where the template will be rendered upon

        skills : 
            [{
                "skillCode": "Mechanical Engineer",
                "skillDescription": "Mechanical Engineer"
            },
            {
                "skillCode": "Air Conditioning Engineer",
                "skillDescription": "Air Conditioning Engineer"
            },
            {
                "skillCode": "Electrical Engineer",
                "skillDescription": "Electrical Engineer"
            }],



        template: _.template(SkillsTemplate),

        // setup the view to listen to its counterpart model for changes
        initialize: function ()
        {
            this.render();
        },

        // render template onto the view
        render: function ()
        {
            this.$el.html(this.skills);
            return this;
        }
    });

    return FilterView;
});

模板

<div id="test">
      <% console.log(skills); %>
</div> 

错误

ReferenceError: skills is not defined

console.log(skills);

我看了几个 Backbone 教程,我看不出我犯了什么错误。

您需要调用 .template 方法

this.$el.html(this.template({skills: this.skills}));

_.template

您应该将上下文传递给您的模板,而不是 jQuery 的 html 方法:

this.$el.html(this.template({ skills: this.skills }));