主干嵌套视图找不到 id 选择器
Backbone nested view unable to find id selector
在下面的代码中,我使用元素控制器模式来呈现产品集合。主视图模板渲染正确,我可以看到所有元素和div 选择器"#cart 包装器"。显然,当主视图使用"addOne"调用嵌套视图时,它找不到上面的选择器:
directory.CartView = Backbone.View.extend({
initialize: function(options) {
this.collection = directory.shellView.cartcollection;
},
render: function(){
this.$el.html(this.template());
this.addAll();
return this;
},
addAll: function() {
this.collection.each(this.addOne, this);
},
addOne: function(model) {
directory.cartItemView = new directory.CartItemView({model: model});
directory.cartItemView.render();
$("#cart-wrapper").append(directory.cartItemView.el);
}
});
嵌套视图
directory.CartItemView = Backbone.View.extend({
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
在addOne函数中,$("#cart-wrapper").length==0 。我做了console.log(directory.cartItemView.el),下划线模板似乎可以与表格内渲染的所有模型一起使用。
主视图是这样调用的:
directory.cartView = new directory.CartView();
directory.cartView.render();
$("#content").html(directory.cartView.el);
这是因为您在将视图的根元素添加到页面之前调用$("#cart-wrapper")
,而这正是jQuery
正在寻找它的地方。要修复它,只需调用this.$("#cart-wrapper")
。
相关文章:
- 基于类似索引的queryString的Id选择器
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- 如何在jQuery的.on()方法中动态更新按钮的ID选择器
- Jquery语法错误,id选择器上的表达式无法识别
- JavaScript id 选择器不起作用?不知道出了什么问题
- 将 ID 选择器注入样式
- Jquery id 选择器变量无法绑定到 onchange
- jQuery ID 选择器仅适用于第一个元素
- JavaScript 将 body_0$ 添加到 ID 选择器中
- 使用隐藏字段 ID 选择器加载另一个字段中的文本
- jquery id 选择器的问题
- jQuery id 选择器在
- 如何在 ID 选择器中看到通配符
- j查询多个和不断变化的 ID 选择器
- 用于使用 javascript 匹配 id 选择器的正则表达式
- 主干嵌套视图找不到 id 选择器
- CSS id 选择器根本不起作用
- 选择具有以某个字符串开头的 ID 选择器的下拉列表
- Jquery不处理带有ID选择器的匿名函数调用
- jQuery ID选择器在I'I’我正盯着大自然看