我創建了一個幫助器,用於在Meteor應用程序中顯示通用模態(在coffeescript中)。如何在流星中使用回調?
這裏的modal.coffee:
showModal = (modalType, title, body, callback) ->
validModals = [ "Error", "YesNo" ]
if not modalType in validModals
alert "Invalid modal type specified" # @todo - find a better way of handling this error
Session.set "modalType", modalType
Session.set "modalTitle", title or ""
Session.set "modalBody", body or ""
modalCallback = callback or undefined
Session.set "showModal", true
Template.modal.title =() ->
Session.get "modalTitle"
Template.modal.body =() ->
Session.get "modalBody"
Template.modal.response =() ->
switch Session.get "modalType"
when "Error"
[{
css: 'cancel',
message: 'OK'
}]
when "YesNo"
[
{
css: 'cancel',
message: "No"
},
{
css: 'btn-primary',
message: "Yes"
},
]
Template.page.shouldShowModal =() ->
Session.get "showModal"
Template.modal.events {
'click .cancel': ->
Session.set "showModal", false
cb = modalCallback
alert "here " + cb
if cb
cb(false)
'click .btn-primary': ->
Session.set "showModal", false
cb = Session.get "modalCallback"
if cb
cb(true)
}
模板也挺無聊的。
這裏是我的客戶端代碼(如本助手的調用):
Template.details.events {
'click .remove':() ->
showModal "YesNo",
"Are you sure you want to delete this item?",
"Deleting an items can't be undone. Are you sure you want to delete?",
(response) =>
if response
Items.remove this._id, (err) =>
if err
showModal "Error", "Error removing item", err.reason
return false;
}
我不能讓它執行回調。我見過的所有例子最終都會將所有內容放入會話中,但顯然它不能將該函數轉換爲json,因此當用戶單擊ok或cancel按鈕時,它不會正確地反序列化它。
如何在用戶響應我的模態時執行回調?
締約方搬到這裏:https://開頭github.com/meteor/meteor/tree/devel/examples/other/parties –