0

我有一个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}));
            }
        });
    },

但是,lecturerDetails 似乎有问题 - 控制台在 Lecturer.fetch 代码中指示错误:GET localhost/api/lecturers/lecturer 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 %>/lecturers/<%= staffNumber %>'

任何人都可以告诉我是否有任何明显的遗漏 - 我正在尝试很多方法来解决这个问题,但没有取得太大的成功!

谢谢!

4

0 回答 0