【发布时间】:2014-05-29 21:59:28
【问题描述】:
在我的 Backbone 应用程序中,我试图弄清楚为什么当我单击页面上的后退按钮时,URL 会相应更改,但实际浏览器显示却没有。
这是一个流程:
- 前往申请:选举/
- 起始页:选举/#profile/manage/:id/
- 点击的页面:选举/#profile/manage/:id/personalInfo/
- 单击了返回按钮。应该最终显示选举/#profile/manage/:id/
但是当点击后退按钮时,页面显示并没有改变,只是 URL。
我不确定是什么原因造成的,也不确定如何解决这个问题。我已经阅读了有关 Backbone.history.start() 命令选项的一些内容,但是每当我向其中添加任何内容时,都不会显示任何内容。
不太确定如何解决这个问题。有人可以指出我正确的方向吗? (如果需要,我可以扩展代码示例,我只是认为这可能更容易阅读)
elections/app.js - 从elections/index.html调用
require.config({
baseUrl: 'js/lib',
paths: {
app: '../app',
tpl: '../tpl',
bootstrap: 'bootstrap/js/',
},
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'underscore': {
exports: '_'
},
}
});
require([
'jquery',
'backbone',
'app/router',
], function ($, Backbone, Router) {
var router = new Router();
Backbone.history.start({pushState:true});
});
elections/js/router.js - 在 app.js 中实例化
define(function (require) {
"use strict";
var $ = require('jquery'),
Backbone = require('backbone'),
WindowView = require('app/views/Window'),
breadcrumbs = {"Home": ""},
$body = "",
$content = "",
return Backbone.Router.extend({
routes: {
'' : 'home',
'profile/login(/)' : 'candidateProfileLogin',
'profile/manage(/)' : 'candidateProfileLogin',
'profile/manage/:id(/)' : 'candidateProfileHome',
'profile/manage/:id/:section(/)' : 'candidateProfileSection',
},
initialize: function () {
require([], function () {
$body = $('body');
windowView = new WindowView ({el: $body}).render();
$content = $("#content", windowView .el);
});
},
candidateProfileHome: function (id) {
require(["app/views/Candidate", "app/models/candidate"], function (CandidateView, models) {
var candidate = new models.Candidate({id: id});
console.log('router: candidateProfileHome');
candidate.fetch({
success: function (data, response) {
var view = new CandidateView({model: data, el: $content});
view.render();
},
error: function (data, response) {
var view = new CandidateView({model: data, el: $content});
view.render();
}
});
breadcrumbs['Profile Home'] = Backbone.history.fragment;
windowView.addBreadcrumbs(breadcrumbs);
windowView.selectMenuItem('candidate-menu');
});
},
candidateProfileSection: function (id, section) {
require(["app/views/Candidate", "app/models/candidate"], function (CandidateView, models) {
var sectionNames = {
'questionnaire': 'Questionnaire',
'endorsements' : 'Endorsements',
'photo' : 'Photo',
};
var sectionName = sectionNames[section];
var candidate = new models.Candidate({id: id});
candidate.fetch({
success: function (data, response) {
var view = new CandidateView({model: data, el: $content});
view.render(section);
},
error: function (data, response) {
//Output the data to the console. Let the template take care of the error pages
console.log(data);
var view = new CandidateView({model: data, el: $content});
view.render(section);
}
});
breadcrumbs['Profile Home'] = "profile/manage/" + id + "/";
breadcrumbs[sectionName] = Backbone.history.fragment;
windowView.addBreadcrumbs(breadcrumbs);
windowView.selectMenuItem('candidate-menu');
});
},
candidateProfileQuestionnaire: function (id, page) {
require(["app/views/Candidate", "app/models/candidate"], function (CandidateView, models) {
var pageNames = {
'personalInfo': 'Personal Information',
'essay' : 'Biography & Essay',
'survey' : 'Survey Questions',
'endorsements': 'Endorsements',
'photo' : 'Photo'
};
var pageName = "Questionnaire: " + pageNames[page];
var candidate = new models.Candidate({password: id});
candidate.fetch({
success: function (data, response) {
console.log('success');
var view = new CandidateView({model: data, el: $content});
view.render(page);
},
error: function (data, response) {
//Output the data to the console. Let the template take care of the error pages
console.log('error');
console.log(data);
var view = new CandidateView({model: data, el: $content});
view.render(page);
}
});
breadcrumbs['Profile Home'] = "profile/manage/" + id + "/";
breadcrumbs[pageName] = Backbone.history.fragment;
windowView.addBreadcrumbs(breadcrumbs);
windowView.selectMenuItem('candidate-menu');
});
},
});
});
【问题讨论】:
-
似乎 url 仍然有哈希值,但您正在使用推送状态;你的
elections/#profile路线对我来说似乎很奇怪 - 我错过了什么吗?我会假设elections/profile路线是正确的。 -
对不起,选举是机器上所有内容的文件夹。我应该在一开始就澄清这一点。 :)
-
我真的在看
/#profile中的“#”是哈希片段的开头吗?您不应该使用启用了推送状态的哈希片段。您的服务器端代码是否已正确编码以处理推送状态? -
我不知道?我什至不知道推送状态是做什么的,我只是在尝试不同的方法来尝试解决这个后退按钮问题。如果还不是很明显,我真的不知道我在做什么。我已经使用骨干...... 2 周了?我必须边走边学。
-
重构建议:由于您的 fetch
success和error回调是相同的,请将它们移动到单个fetch.always()块中。您能否在这些回调中设置一个断点,以确保您的$content、data和view.render()返回您期望的结果。