0

我有以下模板代码

<a href="javascript:void(0)" {{bindAttr data-id="voucher.id"}} {{ action 'deleteVoucher' target="controller"}}>Delete</a>

我的控制器代码是

App.CreditIndexController = Ember.ArrayController.extend({
    deleteVoucher: function(event){
        //var g = this.get('data-id');
        //var g = $(event.target).attr('deleteVoucher');
        var g = $(this.target);//.attr('data-id');
        console.log(g);
    }    
});

我刚从控制器中的模板中获取“data-id”的值。我已经尝试了 deleteVoucher 函数中的代码。但没有一个奏效。

4

2 回答 2

1

使用 Ember,您不必这样做,相反,您可以轻松地将对象传递给您的操作助手,该对象可作为参数提供给您的操作处理程序:

<a href="#" {{ action deleteVoucher voucher target="controller"}}>Delete</a>


App.CreditIndexController = Ember.ArrayController.extend({
    deleteVoucher: function(voucher){
        var id = voucher.get("id")
        console.log(id);
    }    
});
于 2013-03-30T08:26:37.437 回答
1

这比你想象的要容易:

<a href="#" {{action 'deleteVoucher' voucher}}>Delete</a>

控制器:

App.CreditIndexController = Ember.ArrayController.extend({
  deleteVoucher: function(voucher){
    console.log(voucher.get("id"));
  }    
});
  1. 您不需要专门针对控制器,因为它是{{action}}s的默认目标
  2. 不要将数据 ( voucher.id) 存储在 DOM 中。这是一个不好的做法。将 DOM 视为您在应用程序中其他地方的反映。
  3. It doesn't matter what you put into the href attribute when you're using an action on that element: it'll automatically prevent the link to be visited when clicked.
于 2013-03-30T08:27:09.143 回答