简体   繁体   English

在骨干网中触发路由的最佳方法是什么?

[英]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.

相关问题 将2条路由设置到同一组件的最佳方法是什么 - What is the best way to set 2 routes to the same component 使用backbonejs视图,将“onload”事件附加到图像标记的最佳方法是什么? - Using backbonejs view, what is the best way to attach an “onload” event to an image tag? 将/.json添加到快速路由末尾的最佳方法是什么? - What is the best way to add /.json to the end of express routes? 在 react-router-dom 中分离路由的最佳方法是什么? - What's the best way to separate the routes in react-router-dom? 什么是将现有的bonesJS应用程序转换为angularJS应用程序的最佳方法 - what are the best approach to convert the existing backboneJS app into angularJS app 构造MongoClient和表达的最佳方法是什么:路由内的客户端还是客户端内的路由? - What's the best way to structure MongoClient and express: Client inside routes or routes inside client? 防止同一触发函数执行两次的最佳方法是什么? - What is the best way to prevent the same trigger function from executing twice? 使缩放事件自动触发调整大小事件的最佳方法是什么? - What is the best way to make a zoom event automatically trigger a resize event? 处理侦听并触发事件的元素/对象的最佳方法是什么? - What is the best way to deal with elements/objects that listen AND trigger events? 从父组件触发子组件的最佳方式是什么? - What's the best way to trigger a child emit from the parent component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM