3

我正在使用 django-tastypie REST API 和 AngularJS 建立一个项目。我可以通过角度从 json 文件中读取内容,但是我找不到一个像样的教程来告诉我如何制作一个简单的 CRUD 应用程序,它不会将所有信息保存在对象或其他任何东西中,而是在进行操作通过tastepie api 的数据库。你们中的任何人都可以给我看这样的教程,或者只是给我看一些示例代码吗?

谢谢你。

4

1 回答 1

0

使用$resource - 创建资源对象的工厂,可让您与 RESTful 服务器端数据源进行交互。

假设您有 Django 模型Book和名为BookResource的美味派资源。它的 URL 是 /api/v1/book/。如您所知,此 URL 实际上是一种资源,这意味着您可以使用 GET、POST、DELETE 等请求来操作 Book 模型中的数据。您可以通过以下方式将 Angular $resource “映射”到此 API 资源:

someModule.factory('bookResource', ['$resource', function($resource) {
    var apiResourceUrl = "/api/v1/book/:bookId/";
    // id - your model instance's id or pk, that is represented in API resource objects.
    var resource = $resource(apiResourceUrl, {bookId: '@id'}, {
        all: {
            method: 'GET', params: {}, // GET params that will included in request.
            isArray: true, // Returned object for this action is an array (miltiple instances).
        },
        get: {
            method: 'GET',
        },
        // [Define custom save method to use PUT instead of POST.][2]
        save: {
            /* But, the PUT request requires the all fields in object.
            Missing fields may cause errors, or be filled in by default values.
            It's like a Django form save.
            */
            method: 'PUT',
        },
        // [Tastypie use POST for create new instances][3]
        create: {
            method: 'POST',
        },
        delete: {
            method: 'DELETE',
        },
        // Some custom increment action. (/api/v1/books/1/?updateViews)
        updateViews: {
            method: 'GET',
            params: {"updateViews": true},
            isArray: false,
        },
    });
}]);

someModule.controller('bookCtrl', ['$scope', '$routeParams', 'bookResource',
  function ($scope, $routeParams, bookResource) {
    if ("bookId" in $routeParams) {
        // Here is single instance (API's detail request)
        var currentBook = bookResource.get({bookId: $routeParams.bookId}, function () {
            // When request finished and `currentBook` has data.

            // Update scope ($apply is important)
            $scope.$apply(function(){
                $scope.currentBook = currentBook;
            });

            // And you can change it in REST way.
            currentBook.title = "New title";
            currentBook.$save(); // Send PUT request to API that updates the instance
            currentBook.$updateViews();
        });
    }

    // Show all books collection on page.
    var allBooks = bookResource.all(function () {
        $scope.$apply(function(){
            $scope.allBooks = allBooks;
        });
    });

    // Create new
    var newBook = new bookResource({
        title: "AngularJS-Learning",
        price: 0,
    });
    newBook.$save();

}]);

Angular 的文档提供了更多关于如何真正令人难以置信地使用资源的信息。

这是网址的问题。我记得,Angular 会向 /api/v1/books/1 发送请求(最后没有斜线),你会从 sweetpie 得到 404。让我检查一下。

[2] http://django-tastypie.readthedocs.org/en/latest/interacting.html#updating-an-existing-resource-put [3] http://django-tastypie.readthedocs.org/en/latest /interacting.html#creating-a-new-resource-post

于 2013-10-24T12:26:34.130 回答