如何在没有Ember数据的情况下设计Ember deleteRecord方法?

时间:2014-04-22 06:35:31

标签: ember.js model controller destroy

我的Ember版本:

DEBUG: ------------------------------- 
DEBUG: Ember      : 1.5.0 
DEBUG: Handlebars : 1.3.0 
DEBUG: jQuery     : 1.11.0 
DEBUG: ------------------------------- 

我像这样设计我的模型:

App.Product = Em.Object.extend
  isNew: (->
    not @get('id')
  ).property('id')

  save: ->
    if @get('isNew')
      url = "/api/products.json"
      method = 'POST'
    else
      url = "/api/products/#{@get('id')}.json"
      method = 'PATCH'

    $.ajax(url: url, type: method, data: @paramsForSave()).then(
      (payload) =>
        @setProperties(payload.product)
      (xhr) =>
        xhr.responseJSON.errors
    )

  deleteRecord: ->
    $.ajax(url: "/api/products/#{@get('id')}.json", type: "DELETE").then (payload) =>
      @destroy

  paramsForSave: ->
    keys = ['name']
    params = @getProperties(keys)
    params[key] = null for key in params when params[key] == undefined

    {product: params}

App.Product.reopenClass
  _findAll: (params) ->
    modelClass = @
    $.get("/api/products.json", params).then (payload)->
      payload.products.map (obj)-> 
        modelClass.create(obj)

  findAll: (params) ->
    RepairFacility.PromiseArray.create promise: @_findAll(params)

  _find: (id)->
    modelClass = @
    $.get("/api/products/#{id}.json").then (obj) ->
      modelClass.create(obj.product)

  find: (id) ->
    RepairFacility.PromiseObject.create promise: @_find(id)

模型正常工作。但是当我使用deleteRecord来破坏我的模型时,如果我不能transitionToRoute,则模型会在数据库中被销毁,但也会在页面中销毁。
问题是如何从我的arrary控制器或对象控制器中删除此模型?

示例:

Model Order has many Product

所以我想在订单'show'页面中删除Product。订单'显示'页面:

<div class="row">
  <div class="col-md-12">
    <section class="widget">
      <header>
        <h4>
          Number: {{number}}
        </h4>
      </header>
      <div class="body">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>Name</th>
            </tr>
          </thead>
          <tbody>
            {{#each products itemController='product'}}
              <tr>
                <td>{{name}}</td>
                <td>
                  <a href="#" {{action "destroy"}} class="btn btn-danger btn-xs">
                    {{fa-icon 'trash-o'}}
                  </a>
                </td>
              </tr>
            {{/each}}
          </tbody>
        </table>
      </div>
    </section>
  </div>
</div>

ProductController是:

App.ProductController = Em.ObjectController.extend
  needs: ['order']

  init: ->
    @set("product", App.Product.create(@content))

  actions: 
    destroy: ->
      if confirm("Are you sure?")
        @product.deleteRecord().then ()=>
          @destroy

我尝试使用removeObjectremove。所有这一切都行不通。数据从数据库中删除,但产品也在首页中。

问题在于我的模型设计是什么?
谢谢!

0 个答案:

没有答案