Javascript 如何通过主干网中的路由器指定索引id是个困扰我的问题
我正在尝试建立一个最简单的主干应用程序,通过一组图像进行迭代 我希望以下URL影响下面的应用程序,并已将/???在问题所在的评论中: 网址: 代码: 查看代码和路由器:Javascript 如何通过主干网中的路由器指定索引id是个困扰我的问题,javascript,backbone.js,Javascript,Backbone.js,我正在尝试建立一个最简单的主干应用程序,通过一组图像进行迭代 我希望以下URL影响下面的应用程序,并已将/???在问题所在的评论中: 网址: 代码: 查看代码和路由器: arc.ItemView = Backbone.View.extend({ el: $('#mig-container'), initialize:function(){ this.model=this.collection.at(0); this.render(this.model);
arc.ItemView = Backbone.View.extend({
el: $('#mig-container'),
initialize:function(){
this.model=this.collection.at(0);
this.render(this.model);
},
render:function(){
var html=this.model.get('url');
this.$el.html(html);
return this;
}
});
var imgView=new arc.ItemView({collection:imgs});
var HexApp = Backbone.Router.extend({
routes: {
"": "startapp",
"image/:index_id": "image"
},
startapp:function(){
console.log("within startapp");
imgView.render();
},
image:function(index_id){
console.log("within image: " + index_id);
// get Uncaught TypeError: Cannot call method 'at' of undefined in Chrome
imgView.model=this.collection.at(index_id); //??? no idea how to manipulate this
}
});
hex={};
var app_router = new HexApp();
Backbone.history.start();
如何将集合设置在索引\u id指定的索引处?还是有更惯用的方法来说明这一点
thx提前如果我正确理解了你的问题,你有一个应用程序,它的初始状态会渲染集合中的第一幅图像。点击
图像
路径时,您希望使用所选图像重新渲染imgView
,对吗
只要看一下代码,你就差不多明白了。您最不需要做的事情是在更新模型后重新渲染视图:
image:function(index_id){
console.log("within image: " + index_id);
imgView.model=this.collection.at(index_id);
// re-render
imgView.render();
}
否则,我认为对于一个示例应用程序来说,该结构很好。您需要修复
imgsArray
中第二项的声明,因此关键是url
vs.img
。) 路由器无法解析此集合;这是指路由器实例中未定义的集合
属性。相反,您需要使用以下选项:
imgView.model = imgs.at(index_id);
imgs
是您早期用于存储所定义集合实例的变量。thx Herman,这解决了这个问题,是有更好的方法来实现还是可以实现?我认为这应该可以,我自己也可以实现,但我仍在学习。如果你不想在路由器中加入这么多逻辑,你可以查看像木偶网这样的主干库;需要使用除此之外的东西。收藏。哦,这是问题吗@赫尔曼特兰的回答应该能解决这个问题。
image:function(index_id){
console.log("within image: " + index_id);
imgView.model=this.collection.at(index_id);
// re-render
imgView.render();
}
imgView.model = imgs.at(index_id);