2014-10-26 70 views
0

我是一个Ember noob,并且在使用ember/rails应用程序时遇到了一些麻烦。我相信我有,我不是很清楚如何处理范围的问题..如何使用Ember.js和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'。但是,我不知道如何解决这个问题......如何在函数中引用控制器?有关如何纠正这个或更好的方式来实现相同功能的任何建议?

回答

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") 
) 
+0

我不认为你能在余烬控制器虽然声明变量...我刚刚试了一下,以确保和出错了。 – Swappticon 2014-10-27 18:16:46

+0

你是什么意思?你可以'var controller = this;'或者任何coffescript相当于它的。 – Asgaroth 2014-10-27 18:19:56

+0

好的,我明白了!谢谢你的提示。我意识到我必须在删除操作中而不是在通用控制器中创建控制器变量。 – Swappticon 2014-10-28 04:34:36

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;    
     } 
    }); 
}); 
+0

谢谢Jai先生....问题解决了 – 2016-08-09 07:36:34

+1

我在2小时的研究和谷歌以及以后得到了这个:P – 2016-08-09 07:39:18