执行与AngularJS的NG-资源请求的秩序? [英] Order of executing requests with AngularJS's ng-resource?

查看:156
本文介绍了执行与AngularJS的NG-资源请求的秩序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

books_ctrl.js.coffee

myApp.controller "BooksCtrl", ($scope, Book) ->
  $scope.getBooks = () ->
    $scope.books = Book.query()

A部分:初始code:

Part A: Initial Code:

  $scope.delete = (book) ->
    book.$delete()
    $scope.getBooks()

A部分:由@apneadiving解决方法:

  $scope.delete = (book) ->
    book.$delete {}, ->
      $scope.getBooks()
      return

B部分:初始code:

Part B: Initial Code:

  $scope.save = () ->
    if $scope.book.id?
      Book.update($scope.book)
    else
      Book.save($scope.book)
    $scope.book = {}
    $scope.getBooks()

B部分:解决未决:我怎么能告诉角先完成保存或分别只完成后更新getBooks开始()?请参考<一个href=\"http://stackoverflow.com/questions/24552886/order-of-executing-requests-with-angularjss-ng-resource-pt2\">this.

Part B: Solution pending: How can I tell Angular to first complete the save or update respectively and only upon completion start with getBooks() ? Please refer to 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执行这样 - 请求,在我的案件的不良影响。

Here is my problem: AngularJS seems to execute all requests at the same time. That way the GET-Request is executed prior to the POST-Request, an undesirable effect in my case.

我怎么能告诉角度来执行 GET POST 完成后才?

How can I tell Angular to execute the GET only after POST is completed?

推荐答案

我会怎么做:

book.$delete({}, function(){ $scope.getBooks(); })

甚至

book.$delete({}, $scope.getBooks)

所以,你有秩序的完全控制

So you have full control of the order

这篇关于执行与AngularJS的NG-资源请求的秩序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆