0

当我使用 @model.destroy 方法删除对象时,它会尝试重定向到已删除的对象。如何更改路由以重定向到根目录?

我的删除方法:

  deleteBook: (ev) ->
    @model.destroy success: ->
      router.navigate "",
        trigger: true

路由器:

Router = Backbone.Router.extend(routes: 
  "": "home"
)

更新:

完整错误:

GET http://localhost:3000/ 404 (Not Found) jquery.js?body=1:8707
send                                       jquery.js?body=1:8707
jQuery.extend.ajax                         jquery.js?body=1:8137
Backbone.sync                              backbone_rails_sync.js?body=1:65
_.extend.destroy                           backbone.js?body=1:428
Backbone.View.extend.deleteBook            main.js?body=1:32
jQuery.event.dispatch                      jquery.js?body=1:5096
elemData.handle

更新2:

我的咖啡脚本文件:

  $(document).ready ->
    window.Book = Backbone.Model.extend(
      urlRoot: '/books/', idAttribute: 'id'
    )


    BooksView = Backbone.View.extend(
      el: ".books"
      addOne: (model) ->
        view = new BookView(model: model)
        $("ul.books").append view.render()
    )

    Router = Backbone.Router.extend(routes: 
      "": "home"
    )

    Books = Backbone.Collection.extend(
      model: Book

      url: '/books/'
    )



    BookView = Backbone.View.extend(
      tagName: "li"
      events:
        "click .delete": "deleteBook"

      deleteBook: (ev) ->
        @model.destroy()

      render: ->
        #@$el.append('Some text <button type="button" class="delete">Delete</button>')
        title = @model.get("title")
        author = @model.get("author")
        year = @model.get("year")
        link = "books/"+@model.get("id")
        a = document.createElement('a')
        a.href = link
        show = '<a href="'+a+'">Show</a>'       
        row = title + ", " + author + ", " + year + " " + show
        $(@el).html row
        @$el.append(' <button type="button" class="delete">Delete</button>')


    )

    books = new Books()
    books.url = "books.json"
    books.fetch success: ->
      books_view = new BooksView({})
      _.each books.models, (model) ->
        books_view.addOne model

看起来它甚至没有看到成功函数,要么它试图重定向到被删除对象的路径。

或者它仍然引用已删除的对象,并且在删除后尝试使用 GET 方法?

4

0 回答 0