Backbone.js _.每个都在BackboneJS中
_.在上述代码中,每个不旋转 更新 下面是我的子菜单jsonBackbone.js _.每个都在BackboneJS中,backbone.js,Backbone.js,_.在上述代码中,每个不旋转 更新 下面是我的子菜单json var MenuListView = Backbone.View.extend({ el : '#menus', reset : function() { var hIndex = this.getTheMenuIndexHighlighting(); _.each(this.model.models[hIndex].att
var MenuListView = Backbone.View.extend({
el : '#menus',
reset : function() {
var hIndex = this.getTheMenuIndexHighlighting();
_.each(this.model.models[hIndex].attributes.subMenus, function(model, index) {
model.set({highlight : false});
});
});
有人能告诉我为什么吗
将u.each替换为$。each会旋转循环,但不会触发相应的模型视图更新方法。显然,答案必须通过猜测得出,因为您发布的代码数量很少。但这是我的尝试
- 我想这应该是
转换为this.model.models[hIndex].attributes.subMenus
假设this.model引用的集合肯定具有models属性this.collection.models[hIndex].attributes.subMenus
- 其次,如果您需要索引处的模型,则集合中有一个方法,从而简化为this.collection.at(hIndex).attributes,子菜单
- 第三,由于我们使用了collection(指子菜单),您现在可以执行此操作
this.collection.at(hIndex).get(“子菜单”).each(函数(子菜单){alert(subMenu.toJSON();})
var viewInstance=new MenuListView({collection:Menu})
其中Menu是集合(您使用的)的实例(
new
)您应该更改此行
var MenuListView = Backbone.View.extend({
el: '#menus',
reset: function () {
var hIndex = this.getTheMenuIndexHighlighting();
this.collection.at(hIndex).get("subMenus").each(function (subMenu) {
alert(subMenu.toJSON());
})
});
到
编辑:-通过这样说,我假设子菜单本身就是一个集合但现在看来,它是一个数组,下面划线的站点是。\each()不可用于数组,只能用于集合。其中,作为jquery,每个都可以应用于数组。
链接到下划线网站请注意,每个数组都不包含子菜单子菜单是什么样子的?[{title:'Applications',bmUrl:'',id:'1',子菜单:[{title:'MsWord',bmUrl:'',id:1,子菜单:null},{title:'MsExcel',bmUrl:'',id:2,子菜单:null},{title:'MsPP',bmUrl:'',id:3,子菜单:null},{title:'MsOneNote',bmUrl:'',id:4,子菜单:null},{title:'MsOutlook',bmUrl:'',id:5,子菜单:null}],imgUrl:'']为了澄清起见,您发布了
this.model
的数据,还是this.model.models[hIndex]中的数据
?@jahir使用jsonlint.com检查我们的json。你能提供一些关于this.model
和this.model.models
的更多信息吗?我相信你在主干中处理模型的方式有问题。你能做一个控制台.log(this.model)吗
并发布它?这个.collection是未定义的。@jahirhstu我确实指出了一点,我假设model
在这里是collection
,这意味着在创建视图时,要做这件事newviewInstance({collection:Menu});
非常感谢。我将这个.model.models[hIndex].attributes.subMenus更改为this.model.at(hIndex)。获取('subMenus')成功了。但我不确定我的代码为什么不起作用。请解释一下。@jahirhstu看到我更新的答案,你的代码似乎是正确的,但我无法确认,除非我看到全局(你所有的代码)下划线的each确实通过数组进行迭代。下划线网站上的第一个示例使用数组上的each函数:但是它不是数组原型的一部分。
var MenuListView = Backbone.View.extend({
el: '#menus',
reset: function () {
var hIndex = this.getTheMenuIndexHighlighting();
this.collection.at(hIndex).get("subMenus").each(function (subMenu) {
alert(subMenu.toJSON());
})
});
this.model.models[hIndex].attributes.subMenus
this.model.at(hIndex).get("subMenus")