我有一个backbone.js + slim php + jquery移动应用程序。
我搜索大学模块,选择一个,然后点击链接以生成与模块相关的讲师列表。这有效!
现在我希望能够点击一个讲师列表项目来制作一个讲师详细信息页面,我得到的是一个我无法弄清楚的404。
我认为我在如何路由到讲师的详细信息方面有问题
我的API方法是:
$app->get('/modules', 'getModules');
$app->get('/modules/:id', 'getModule');
$app->get('/modules/search/:query', 'getModulesByName');
$app->get('/modules/:id/lecturers', 'getLecturers');
$app->get('/modules/:id/lecturers/:staffNumber', 'getLecturer');
这里我定义了getLecturers - 提取与模块相关的讲师列表:
function getLecturers($id) {
$sql = "select l.staffNumber, l.firstName, l.lastName, l.moduleNo1, l.moduleNo2, l.email, m.moduleNo
from lecturertable l, moduletable m
where m.moduleNo=:id AND (l.moduleNo1=:id OR l.moduleNo2=:id)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$lecturer = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
if (!isset($_GET['callback'])) {
echo json_encode($lecturer);
} else {
echo $_GET['callback'] . '(' . json_encode($lecturer) . ');';
}
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
在这里,我试图通过传递'staffNumber'来获得一位个别讲师:
function getLecturer($staffNumber) {
$sql = "select l.staffNumber, l.firstName, l.lastName, l.moduleNo1, l.moduleNo2, l.email
from lecturertable l
where l.staffNumber=:staffNumber";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("staffNumber", $staffNumber);
$stmt->execute();
$lecturer = $stmt->fetchObject();
$db = null;
if (!isset($_GET['callback'])) {
echo json_encode($lecturer);
} else {
echo $_GET['callback'] . '(' . json_encode($lecturer) . ');';
}
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
到目前为止一直很好 - 我想!
我现在尝试在main.js中设置路线:
"":"list",
"list":"list",
"modules/:id":"moduleDetails",
"modules/:id/lecturers":"moduleLecturers",
"modules/:id/lecturers/:staffNumber":"lecturerDetails"
并为最后两条路线提供了几个匹配的功能:
moduleLecturers:function (id) {
var module = new Module({id:id});
module.lecturers.fetch();
this.changePage(new ModuleLecturersPage({model:module.lecturers}));
},
lecturerDetails:function (staffNumber) {
var lecturer = new Lecturer({staffNumber:staffNumber});
var self = this;
lecturer.fetch({
success:function (data) {
self.changePage(new LecturerView({model:data}));
}
});
},
讲座详情看起来有些问题 - 控制台在讲话时指示错误:获取代码:GET localhost / api /讲师/讲师404(未找到)。任何人都可以指出我正确的方向吗?
我的讲师模型是:
window.Lecturer = Backbone.Model.extend({
urlRoot:"../api/lecturers/lecturer",
initialize:function () {
this.lecturer = new lecturerCollection();
this.lecturer.url = '../api/modules/lecturers/' + this.staffNumber + '/lecturer';
}
});
window.lecturerCollection = Backbone.Collection.extend({
model:Lecturer,
url:"../api/lecturers/lecturer",
});
观点如下:
window.LecturerView = Backbone.View.extend({
initialize:function () {
this.template = _.template(tpl.get('lecturer-details'));
},
render:function (eventName) {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
我链接到讲师列表项目中的讲师详细信息模板,其中包含以下链接:href ='#modules /<%= moduleNo%> /讲师/<%= staffNumber%>'
任何人都可以告诉我是否有任何明显的遗漏 - 我正在尝试很多方法,但我没有取得太大的成功!
谢谢!