将数据传递给下划线以使渲染不起作用

时间:2014-10-14 17:59:16

标签: backbone.js underscore.js underscore.js-templating

我正在尝试渲染下面的下划线模板

    div.col-md-12#english
        select(value="", class="form-control")
            option 
                    | Select Your Language Preference
            script(type="text/template" id="english-pref")
             <% if (selected === "US") { %>      
             option(value="US", selected) | United States
             <% } else %>
             <% if(selected === "US"){ %> 
             option(value="UK", selected) | United Kingdom
             <% } %> 

这是我的Backbone View代码

 app.NotesView = Backbone.View.extend({
    el: '#notes',
    events: {
        'click #save': 'save',
        'click #update': 'update'
    },
    template1: _.template($('#about').html()),
    template2: _.template($('#facts').html()),
    template3: _.template($('#english').html()),

    initialize: function() {
        app.NotesModel = new app.NotesModel({});

        this.model = app.NotesModel;

        app.email = $('#data-user').text();
        app.username = $('#data-username').text();

        app.NotesModel.fetch({
            data: {
                email: app.email,
                username: app.username
            },
            type: 'POST',
            processData: true,
            success: function(data) {                    
                $('#save').remove();
            },
            complete: function(xhr, textStatus) {
                //console.log(textStatus);
            },
            error: function() {
                $('#about-container .note-editable').html('');
                $('#note-container .note-editable').html('');
                $('#update').remove();
            }

        });

        this.model.bind('sync', this.render, this);

    },
    render: function() {            
        this.$('#aboutParent').html(this.template1, this);
        this.$('#noteParent').html(this.template2, this);
        app.initializeEditor();
        $('#about-container .note-editable').html(this.model.attributes.about);
        $('#note-container .note-editable').html(this.model.attributes.editorNote);
        if(this.model.attributes.english === null || this.model.attributes.english === undefined || this.model.attributes.english === '') {
            /*var data = '<option value="US">United States</option><option value="UK">United Kingdom</option>';*/ 
            var data = {"selected": "US"};                

            this.$('#noteParent').html(this.template3,data);

        }else {
            var data = {"selected": "UK"};                

            this.$('#noteParent').html(this.template3,data);
        }
    }
 });

我看了几个教程,我真的很困惑,因为每个人都有自己的方法来完成工作。我现在面临的问题是我的模板没有呈现,因为它说选择是未定义的。我是否正确地将对象传递给视图?

我也可以使用一种模式作为渲染模板的经验法则。

1 个答案:

答案 0 :(得分:1)

this.template3(以及template1template2就此而言)是一个函数,您可以将数据作为参数调用以获取HTML。一般来说:

var tmpl = _.template(template_source_text);
var html = tmpl(template_data);

您只是将模板函数传递给jQuery的html

this.$('#noteParent').html(this.template3,data);

当你递归html一个函数时,它调用函数但不调用模板函数所期望的参数:

  

.html(功能)
  类型:函数(整数索引,htmlString oldhtml)=&gt; htmlString

     

返回要设置的HTML内容的函数。接收集合中元素的索引位置和旧HTML值作为参数。

你所做的就像说:

this.$('#noteParent').html(this.template3(some_index, some_html));

你想说:

this.$('#noteParent').html(this.template3(data));

以便将data中的内容传递给模板函数。