Javascript 如何使用backbone.js从视图中的路由器获取ID值
我有一个router.js和一个view.js 我想要在我的视图页面中从router.js页面传递的ID值。我怎么能得到这个 这是我的路由器: 在我看来,我需要在html元素中设置id的值 这是我的看法 /** *由惠普于2014年6月17日创建。 */Javascript 如何使用backbone.js从视图中的路由器获取ID值,javascript,backbone.js,backbone-views,backbone-routing,Javascript,Backbone.js,Backbone Views,Backbone Routing,我有一个router.js和一个view.js 我想要在我的视图页面中从router.js页面传递的ID值。我怎么能得到这个 这是我的路由器: 在我看来,我需要在html元素中设置id的值 这是我的看法 /** *由惠普于2014年6月17日创建。 */ 请尝试以下代码 color: function(id){ console.log("color called"+id); viewPage = new ViewPage({model: new Model({'id':id})}) viewPa
请尝试以下代码
color: function(id){
console.log("color called"+id);
viewPage = new ViewPage({model: new Model({'id':id})})
viewPage.render(id);
}
我想你可以试着把你所有的论点作为选项来传递 路由器 看法
我遗漏了什么,或者您只需要在视图上创建一个setColor方法,然后在路由器中调用viewPage.setColorid;viewPage.render?你能把你的浏览代码也贴出来吗?您的思路是正确的,但是如果我们也能看到您的视图,那么就更容易给出代码示例。@BGR如果这是set方法,那么视图中的get机制是什么?@AlexP感谢您的回答。这是我的视图代码:var app=app | |{};app.LibraryView=Backbone.View.extend{el:'ulMenu',初始化:functionoptions{this.collection=new app.Library;this.collection.fetch{reset:true};this.render;this.listenTo this.collection,'reset',this.render;},事件:{'click':'show'},show:function{this.subRender;},render:function{},subRender:function{$content.htmlColor Name:;//我需要这里的id。};谢谢。我认为这是传递值的方式。那么下一步是什么,即如何在我的视图页面中获取值?请使用下面的行获取视图中的“id”。[如果喜欢我以前的答案,请单击“上一步”图标]this.model.get“id”
// site/js/views/library.js
var app = app || {};
app.LibraryView = Backbone.View.extend({
el: '#ulMenu',
initialize: function(options) {
this.collection = new app.Library();
this.collection.fetch({reset: true});
this.render();
this.listenTo( this.collection, 'reset', this.render );
},
events:{
'click':'show'
},
show: function(){
this.subRender();
},
render: function() {
},
subRender: function() {
$("#content").html("Color Name: ");
}
});
color: function(id){
console.log("color called"+id);
viewPage = new ViewPage({model: new Model({'id':id})})
viewPage.render(id);
}
var app = app || {};
app.Router = Backbone.Router.extend({
routes: {
'': 'index',
'color/:id': 'color'
},
index: function(){
console.log("index called");
},
color: function(id){
console.log("color called"+id);
var libraryView = new LibraryView({
id : id //passing it as option to views initalize.
});
}
});
new app.Router;
Backbone.history.start();
var app = app || {};
app.LibraryView = Backbone.View.extend({
el: '#ulMenu',
initialize: function(options) {
this.receivedId = this.options.id; // id is catched here
this.collection = new app.Library();
this.collection.fetch({reset: true});
this.render();
this.listenTo( this.collection, 'reset', this.render );
},
events:{
'click':'show'
},
show: function(){
this.subRender();
},
render: function() {
},
subRender: function() {
$("#content").html("Color Name: ");
}
});