2013-03-15 79 views
0

我试图将这个jQuery模板转换为一个underscorejs模板,但没有意义为什么它不工作。谁能解释为什么?从旧的转换下划线模板的错误

 el: $('#contents'), 
    template: _.template(MenuTemplate), 
    //template: $('#item-tmpl').template(), 

    render: function() 
    { 
    this.$el.empty(); 

    //$.tmpl(this.template, this.model.toArray()).appendTo(this.el); 
    // Old jquery template 


    //this.$el.html(this.template(this.model.toArray()).appendTo(this.el)); 
    //underscore template 

     return this; 
    } 
+0

什么不工作?我不明白你使用模板并设置到根元素的技巧(不需要调用appendTo)。请参阅[这里](http://backbonejs.org/#View-render)。 – WiredPrairie 2013-03-15 10:49:25

回答

1

你有点过您使用的模板。试试这个:

el: $('#contents'), 
template: _.template(MenuTemplate), 

render: function() 
{ 
    this.$el.empty(); 

    this.$el.html(this.template(this.model.toArray())); 

    return this; 
} 
+0

以前试过这个,但是在chrome中有一个错误'Uncaught ReferenceError:position is not defined' – koko 2013-03-18 03:29:45

+0

这里是我从backbonejs网站获得的示例https://www.dropbox.com/s/ixk4htvgenx87y0/bbgek.zip – koko 2013-03-18 03:44:03