[英]What is the best way to trigger routes in backbonejs?
There are different ways to invoke routes. 有多种方法可以调用路由。 I currently use Router.navigate( '/url', true ) in my view functions to trigger a route, but it also seems possible to trigger a route with an anchor tag with the url hash as the href attribute. 我目前在我的视图函数中使用Router.navigate('/ url',true)来触发路由,但是似乎也可以使用带有url哈希作为href属性的锚标记来触发路由。
The way I render a collection to the screen has also something to do with it I guess. 我猜我将集合呈现到屏幕上的方式也与此有关。 I have a global 'index' view that render a new subview for each item from the collection. 我有一个全局的“索引”视图,该视图为集合中的每个项目呈现一个新的子视图。
render : function() {
var self = this;
$( this.el ).html( _.template( this.template, {} ) );
this.collection.each( function( model ) {
self.addOne( model );
} );
},
addOne : function( model ) {
var project = new TMRS.views.projects.Record( { model : model } );
$( this.el ).append( project.render().el );
},
Within the subview I trigger a route when a specific element is clicked, it works and it is easy as pie. 在子视图中,当单击特定元素时,我会触发一条路线,该路线有效,并且很容易饼图。 But when I would decide to use anchors with hashes there is nu use for the subview anymore, because I can just iterate over the collections within my templates and handle urls with anchor tags. 但是当我决定使用带有哈希的锚点时,子视图就不再有用,因为我可以遍历模板中的集合并使用锚点标签处理URL。
I think it is better to use a single view for each record, but I don't know if it is the way to go. 我认为为每个记录使用单个视图会更好,但是我不知道这是否可行。 What do you guys think? 你们有什么感想?
I ran into this same issue in my backbone project. 我在骨干项目中也遇到了同样的问题。 I think it all comes down to scalability. 我认为一切都归结为可扩展性。 Do you ever see the subview needing to become more complicated? 您是否看到子视图需要变得更加复杂? If so then it would be better to go ahead keep the subview. 如果是这样,那么最好继续保留子视图。 If you go the templating solution then down the road you would just need to recreate the subview and wire it up again if you needed something more complicated than the anchors. 如果您使用模板解决方案,那么您只需要重新创建子视图,然后在需要比锚点更复杂的东西时再次将其连接起来即可。
In my project, I did the template iterations for dropdowns. 在我的项目中,我为下拉菜单进行了模板迭代。 They will never become more complicated. 它们将永远不会变得更加复杂。 (Most Likely). (最有可能的)。
Most other things I went ahead and did the subview so that I wouldn't have to perform that work later if needed. 我进行了大多数其他操作并进行了子视图,因此以后不需要时就不必执行该工作。 Also it doesn't really take to long to implement. 同样,它并不需要很长时间就能实现。
That's my 2 cents, but really I don't think there is a wrong way. 那是我的2美分,但实际上我认为没有错误的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.