【发布时间】:2017-08-03 10:43:38
【问题描述】:
除了处理其上下文之外,我还试图了解主干bindAll 函数。在这种情况下,以下实现不起作用。
创建一个视图以从列表中插入数据。
_.bindAll (this, 'render');你能在函数中看到this的上下文吗?$(this.el)是undefined。不应该使用_bindAll工作吗?-
以下内容不会添加到
ol列表中$(this.el).append('idCurso->', cursoModelo.get('idCurso'),' titulo-> ', cursoModelo.get('titulo')); $(this.el).append('hola caracola');`.
HTML
<ol id="ListaLibros"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
JS
// create a view to print the model data in the html
var ListaLibrosView = Backbone.View.extend({
el: '#ListaLibros',
initialize: function() {
this.collection = cursoCollection;
_.bindAll(this, 'render');
this.render();
},
render: function() {
console.log("funcion render");
var listaLibrosLi = '';
this.collection.each(function(cursoModelo) {
$(this.el).append('idCurso->', cursoModelo.get('idCurso'), ' titulo-> ', cursoModelo.get('titulo'));
$(this.el).append('hola caracola');
});
}
});
【问题讨论】:
-
请在发布前正确格式化代码...
-
当你可以说
this.$el时不要说$(this.el)。 Backbone 将 jQuery 包装的el缓存在$el中,因此您应该使用它。