使用阵列控制器在Ember中创建/删除用户

时间:2015-03-22 15:08:07

标签: ember.js crud

我是Ember的新手,我正在尝试做一个简单的创建/删除用户。我能够创建一个客户端,但不能删除它们吗?

客户控制器:

export default Ember.ArrayController.extend({
  actions: {
    createClient: function(newName) {
        // Create the new Todo model
        var client = this.store.createRecord('client', {
            name: newName,
            avgMarkup: 2,
            quotes: 1
        });

        // Clear the "New client" text field
        this.set('newName', '');

        // Save the new model
        client.save();
    }
  }
});

然后我尝试添加这个:

    destroyRecord: function() {

        this.get('model').destroyRecord();

    }

我没有运气。我的观点是:

<ul id="client-list">
  <h6>Clients Name:</h6>
    {{input type="text" id="new-client" placeholder="Please enter client name"
    value=newName action="createClient"}}
  {{#each}}
    <li>
      <input type="checkbox" class="toggle">
      <label>{{name}}</label>
      <button {{action "destroyRecord" }} class="destroy"></button>
    </li>
  {{/each}}
</ul>

这是否适用于阵列控制器?

由于

1 个答案:

答案 0 :(得分:2)

要删除相应的用户,只需将其传递到destroyRecord操作:

在模板中传递this,它对应于当前正在迭代的用户:

<button {{action "destroyRecord" this}} class="destroy">Destroy</button>

然后当有人点击销毁按钮时,model将成为当前用户:

destroyRecord: function(model) {
    model.destroyRecord();
}