books_ctrl.js.coffee
myApp.controller "BooksCtrl", ($scope, Book) ->
$scope.getBooks = () ->
$scope.books = Book.query()
A部分:初始代码:
$scope.delete = (book) ->
book.$delete()
$scope.getBooks()
A部分:@apneadiving解决方案:
$scope.delete = (book) ->
book.$delete {}, ->
$scope.getBooks()
return
B部分:初始代码:
$scope.save = () ->
if $scope.book.id?
Book.update($scope.book)
else
Book.save($scope.book)
$scope.book = {}
$scope.getBooks()
B部分:待处理的解决方案:我如何告诉Angular分别首先完成保存或更新,并且只有在完成getBooks()时才会完成?请参阅this。
...
book.js.coffee
myApp.factory "Book", ($resource) ->
$resource("/books/:id", {id: "@id"}, {update: {method: "PUT"}})
books_controller.rb
# GET /books
def index
@books = Book.all
respond_to do |format|
format.html {}
format.json {render json: @books, each_serializer: BookSerializer}
end
end
# DELETE /books/1
def destroy
@book.destroy
respond_to do |format|
format.html {redirect_to books_url, notice: 'Book was successfully destroyed.'}
format.json {render json: {message: "Book was deleted."}}
end
end
Rails服务器开发日志
Started GET "/books" for 127.0.0.1 at 2014-07-03 12:53:07 +0200
Processing by BooksController#index as JSON
Book Load (0.0ms) SELECT "books".* FROM "books"
Completed 200 OK in 2ms (Views: 2.0ms | ActiveRecord: 0.0ms)
Started POST "/books" for 127.0.0.1 at 2014-07-03 12:53:07 +0200
Processing by BooksController#create as JSON
Parameters: {"title"=>"Test", "author"=>"Tester", "book"=>{"title"=>"Test", "author"=>"Tester"}}
(0.0ms) begin transaction
SQL (1.0ms) INSERT INTO "books" ("author", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["author", "Tester"], ["created_at", "2014-07-03 10:53:07.627400"], ["title", "Test"], ["updated_at", "2014-07-03 10:53:07.627400"]]
(23.0ms) commit transaction
Completed 200 OK in 29ms (Views: 1.0ms | ActiveRecord: 24.0ms)
这是我的问题:AngularJS似乎同时执行所有请求。这样GET
- 请求在POST
- 请求之前执行,这在我的情况下是不良影响。
如何在GET
完成后告诉Angular执行POST
?
答案 0 :(得分:0)
我做:
book.$delete({}, function(){ $scope.getBooks(); })
甚至:
book.$delete({}, $scope.getBooks)
因此您可以完全控制订单