0

我是一名 Ember 菜鸟,并且在使用 ember/rails 应用程序时遇到了一些麻烦。我相信我有一个范围问题,我不太确定如何处理..

在我的控制器内部,我有一个删除操作,其中包含以下代码:

swal({
  title: "Are you sure?"
  text: "You will not be able to recover this post!"
  type: "warning"
  showCancelButton: true
  confirmButtonColor: "#DD6B55"
  confirmButtonText: "Yes, delete it!"
  closeOnConfirm: false
}, ->
  @get('model').destroyRecord().then =>
    @transitionToRoute 'posts'
  swal("Deleted!", "Your post has been deleted.", "success")

)

我将错误追溯到'@get('model').destroyRecord().then',我很确定这是因为我在控制器操作内部的一个函数中调用了'this.get'。但是,我不知道如何解决这个问题......如何在函数中引用控制器?有关如何纠正此问题或实现相同功能的更好方法的任何建议?

4

2 回答 2

0

我不知道 coffescript,但也许您可以尝试将控制器分配给变量?

controller = this; // or @ ? 
swal({
  title: "Are you sure?"
  text: "You will not be able to recover this post!"
  type: "warning"
  showCancelButton: true
  confirmButtonColor: "#DD6B55"
  confirmButtonText: "Yes, delete it!"
  closeOnConfirm: false
}, ->
  controller.get('model').destroyRecord().then =>
    controller.transitionToRoute 'posts'
  swal("Deleted!", "Your post has been deleted.", "success")
)
于 2014-10-27T05:08:43.237 回答
0

使用行为:在数据中:

<%= link_to "Delete", s, :method => :delete, data: { behavior: 'delete' } %>

$("[data-behavior='delete']").on("click", function(e){
    e.preventDefault();

    swal({
        title: "Are you sure you want to delete " , 
        text: "You will not be able to recover this data!", 
        type: "warning", 
        showCancelButton: true, 
        confirmButtonColor: "#DD6B55", 
        confirmButtonText: "Yes, delete it!", 
        closeOnConfirm: false 
    }, function(isConfirm) { 
        if (isConfirm)  
        { 
           // here you can use ajax to delete 
           swal("Deleted!", "Ok , rooms will be delete after submit.", "success"); 
        } 
        else  
        { 
           return false;            
        } 
    }); 
});
于 2015-10-26T08:20:19.923 回答