我正在尝试使用Parse.com的parse.js框架设置网站。包含在框架中的是Parse.Router(Backbone.Router的一个分支,但执行时看起来略有不同;但我可能错了)。
我有一些工作正常,但我遇到的问题是,当我在'localhost / root / profile'这样的页面上时,我点击刷新按钮时出现404错误。经过一些搜索后,我采用了修改我的.htaccess文件的方法,这样就可以重定向到我的单页应用程序'index.html'。现在的问题是,当重新加载此页面时,它似乎也会重新启动路由器,从而将我返回到“localhost / root”而不是“localhost / root / 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 :(得分:0)
经过几个小时的玩弄之后,我发现问题并不是我的路由器每次重新加载都重新启动,而是在每次启动时我都在调用:
this.navigate("", { trigger: true });
在我的router.start函数中,无论用户在网站上的哪个位置,都会加载主页。好的,进入下一个难题:)