【问题标题】:How to load this file given a specific Backbone route?如何在给定特定主干路由的情况下加载此文件?
【发布时间】:2014-10-23 05:18:26
【问题描述】:

我目前正在开发一个单页 Web 应用程序,并且是 Backbone 的新手。

我的页面有一个导航列表,每个导航都在一个单独的 PHP/HTML 文件中。我想要的是在给定特定路线的情况下加载此文件(例如,/contacts 将加载 contacts.phpcontacts.html)。

怎么做?

【问题讨论】:

  • 我想要的逻辑或多或少是这样的。 var route = Backbone.Router.extend({ 'contact' : 'contactUsHandler', contactUsHandler : function() { //渲染模板contactus.php或contactus.html } });

标签: backbone.js backbone-routing


【解决方案1】:

我没有找到任何与此相关的论坛或讨论,但是从主干路由渲染模板的过程可以通过

var router = Backbone.Router.extend({
        routes : {
            '/contact' : 'contactUsHandler'
        },
        home : function() {
            var url = urlToRequestServerToRenderATemplate,
                that = this;
            $.ajax({
               type : 'GET',
               url  : url,
               success : function(response) {
                 //response should contain the html rendered from server
                 var contactView = new contactView(); 
                 contactView.render(response.html);
               }
            })
        }
    })

var contactView = Backbone.View.extend({
        el : '#main-container',
        render : function(html) {
            this.$el.html(html);
        }
}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多