delete-button
A Meteor package that provides a delete button UI component. This was formerly provided by the AutoForm package as afDeleteButton
, but it had very little to do with autoform, so it was moved to this separate package.
Installation
In your Meteor app directory, enter:
$ meteor add aldeed:delete-button
Usage
The UI component, quickRemoveButton
, can be used with or without a block:
1{{> quickRemoveButton collection="TestCollection" _id=this._id}} 2 3<!-- OR --> 4 5{{#quickRemoveButton collection="TestCollection" _id=this._id}}Delete Me{{/quickRemoveButton}}
When used without block content, the content of the delete button will be the word "Delete".
At minimum, you need to provide the collection
and _id
attributes:
collection
: Set this to a helper that returns aMeteor.Collection
instance or to a string that identifies aMeteor.Collection
instance that is in thewindow
scope._id
: Set this to the_id
of the document you want the button to remove.
You can optionally provide onError
, onSuccess
, and beforeRemove
attributes, which should be set to helpers that return functions:
onError
: A function that accepts a single argument,error
, and is called only when the remove operation fails. If you don't provide this callback, there is a defaultonError
function that displays an alert and logs the error to the browser console.onSuccess
: A function that accepts a single argument,result
, and is called only when the remove operation succeeds.beforeRemove
: A function that accepts two arguments,collection
andid
, and is called before the document is removed. You can perform asynchronous tasks in this function, such as displaying a confirmation dialog. If the document should be removed, callthis.remove()
.
Example
HTML:
1<template name="docList"> 2 <div class="container"> 3 {{#each docs}} 4 <div class="panel panel-default"> 5 <div class="panel-body"> 6 {{this.name}} | {{> quickRemoveButton collection="Collections.TestCollection" _id=this._id onError=onError onSuccess=onSuccess beforeRemove=beforeRemove class="btn btn-danger"}} 7 </div> 8 </div> 9 {{/each}} 10 </div> 11</template>
JavaScript:
1Collections = {}; 2Collections.TestCollection = new Meteor.Collection("test"); 3 4if (Meteor.isClient) { 5 Template.docList.helpers({ 6 docs: function () { 7 return Collections.TestCollection.find(); 8 }, 9 onError: function () { 10 return function (error) { alert("BOO!"); console.log(error); }; 11 }, 12 onSuccess: function () { 13 return function (result) { alert("YAY!"); console.log(result); }; 14 }, 15 beforeRemove: function () { 16 return function (collection, id) { 17 var doc = collection.findOne(id); 18 if (confirm('Really delete "' + doc.name + '"?')) { 19 this.remove(); 20 } 21 }; 22 } 23 }); 24}
Contributing
Code contributions and fixes welcome by pull request.