2015-08-20 46 views
0

我試圖讓使用Parse.com的parse.js框架建立網站。包含在框架Parse.Router(Backbone.Router的一個分支,但似乎在執行中略有不同,但我可能是錯的)。Parse.Router - 瀏覽器刷新按鈕返回到index.html的

我有事情運作良好,但我遇到問題來了,當我很喜歡「本地主機/根/簡介」頁面上,我點擊刷新按鈕,我得到一個404錯誤。經過一番搜索之後,我採取了修改我的.htaccess文件的方法,以便這會觸發重定向到我的單頁應用程序'index.html'。現在的問題是,當此頁面重新加載它似乎也重新啓動路由器,從而回到我的不是「本地主機/根/ profile文件」「本地主機/根」。誰能告訴我我要去哪裏?

這裏是我的.htaccess文件:

// .htaccess 
<ifModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !index 
    RewriteRule (.*) index.html [L] 
</ifModule> 

這裏是我的app.js文件中,在我的主index.html文件加載:

// app.js 
$(function() { 
    Parse.$ = jQuery; 

    Parse.initialize("xxxx", "xxxx"); 

    // * -------------- 
    // * Navigation Bar 
    // * -------------- 
    var NavigationView = Parse.View.extend({ 
     template: Handlebars.compile($('#navigation-tpl').html()), 
     events: { 
      'click .nav-link': 'link' 
     }, 
     link: function(e) { 
      // Prevent Default Submit Event 
      e.preventDefault(); 
      var href = $(e.target).attr('href'); 
      myRouter.navigate(href, { trigger: true }); 
     }, 
     render: function() { 
      var attributes = this.model.toJSON(); 
      this.$el.html(this.template(attributes)); 
     } 
    }); 

    // * --------- 
    // * Root View 
    // * --------- 
    var RootView = Parse.View.extend({ 
     template: Handlebars.compile($('#root-tpl').html()), 
     events:{ 
     }, 
     render: function() { 
      this.$el.html(this.template()); 
     } 
    }); 

    // * ------------ 
    // * Profile View 
    // * ------------ 
    var ProfileView = Parse.View.extend({ 
     template: Handlebars.compile($('#profile-tpl').html()), 
     events: { 
     }, 
     render: function(){ 
      var attributes = this.model.toJSON(); 
      this.$el.html(this.template(attributes)); 
     } 
    }); 

    // * ------------ 
    // * Parse Router 
    // * ------------ 
    MyRouter = Parse.Router.extend({ 

     // Here you can define some shared variables 
     initialize: function(options){ 
      console.log("initializing router, options: " + options); 
      this.user = Parse.User.current(); 
     },  
     // This runs when we start the router. Just leave it for now. 
     start: function(){ 
      Parse.history.start({ 
       root: "/root/", 
       pushState: true, 
       silent:  false 
      }); 
      this.navigate("", { trigger: true }); 
     }, 
     // This is where you map functions to urls. 
     // Just add '{{URL pattern}}': '{{function name}}' 
     routes: { 
      "":    "root", 
      "profile":  "profile", 
      "*nf":   "notFound" 
     }, 
     root: function() { 
      var rootView = new RootView(); 
      rootView.render(); 
      $('.main-container').html(rootView.el); 
     }, 
     profile: function() { 
      if (this.user) { 
       var profileView = new ProfileView({ model: this.user }); 
       profileView.render(); 
       $('.main-container').html(profileView.el); 
      } else { 
       this.navigate("root", {trigger: true}); 
      } 
     }, 
     notFound: function() { 
      console.log("in the not found"); 
     } 
    }), 
    myRouter = new MyRouter(); 
    myRouter.start(); 

    // * -------------------------- 
    // * Add Navigation to the View 
    // * -------------------------- 
    var user = Parse.User.current(); 
    var navigationView; 
    if (user) { 
     navigationView = new NavigationView({model: user}); 
    } else { 
     navigationView = new NavigationView();  
    } 
    navigationView.render(); 
    $('.navigation-container').html(navigationView.el); 
}); 

我是否正確有此設置?這是我的第一次嘗試(除了遵循一些教程和閱讀文檔之外),所以我可能會錯過一些非常明顯的東西。任何幫助表示讚賞。

回答

0

幾個小時,多用這種玩弄後,我發現,這個問題是不是我的路由器與每個重裝重新啓動,這是每個開始我打電話:

this.navigate("", { trigger: true }); 
在我的路由器

.start函數,無論用戶在網站上的哪個位置,它都會加載主頁。 OK,到下一個謎:)

相關問題