Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干路由和视图状态_Backbone.js_Backbone Routing - Fatal编程技术网

Backbone.js 主干路由和视图状态

Backbone.js 主干路由和视图状态,backbone.js,backbone-routing,Backbone.js,Backbone Routing,为可能措词不当的标题道歉。新的骨干 我很难理解如何处理与视图相关的路由。基本上我有一个视图(我们称之为ListView),它根据其viewMode,使用不同的模板呈现ItemViews。它看起来像这样: var ListView = Backbone.View.extend({ // Cache a bunch of templates here viewMode: 'list', // Default is list render: function() {

为可能措词不当的标题道歉。新的骨干

我很难理解如何处理与视图相关的路由。基本上我有一个视图(我们称之为
ListView
),它根据其
viewMode
,使用不同的模板呈现
ItemViews
。它看起来像这样:

var ListView = Backbone.View.extend({

    // Cache a bunch of templates here

    viewMode: 'list', // Default is list

    render: function() {

        switch(this.viewMode) {
            case 'list':
                // Render ItemView based on list template
                break;

            case 'gallery':
                // Render ItemView based on gallery template
                break;

        }

        // Render all items in list
        this.collection.each(function(model, index) {
            new ItemView(); // Maybe pass viewMode as a parameter
        });

    }

});
我的目标是,无论何时
ListView
使用
viewMode
“列表”或“图库”,这都应该反映在地址栏中,同样地,手动输入或单击指向例如mysite.com/page.html#items/list#items/gallery的链接都应该呈现相同的结果


有没有办法使这个过程自动化,或者以其他方式解决它?

你认为你的路由器应该是这样的:

var yourRouter = Backbone.Router.extend({

    routes: {
        "items/list":    "showList",
        "items/gallery": "showGallery"
    },


    showList: function() {
        listView.viewMode = "list"
        listView.render();
    }

    showGallery: function() {
        listView.viewMode = "gallery"
        listView.render();
    }

});
然后在查看事件中,您可以调用路由器的
导航
方法。这将更新地址栏


yourRouter.navigate(“项目/列表”)

用户是如何在不通过路由器的情况下进入显示列表/图库和该视图的状态的?目前,列表视图仅在AppView中呈现。我慢慢得出结论,这可能根本不可取。