You are not logged in.
Important Note: This forum is being archived and will be completely locked down on the 7th of February. The new forum can be found at http://laravel.io.
This is a quick solution that i've found. I think it would be cool to share here.
Based in the rails.js (the default rails javascript the same as this script among other things)
// restfulizer.js
/**
* Restfulize any hiperlink that contains a data-method attribute by
* creating a mini form with the specified method and adding a trigger
* within the link.
* Requires jQuery!
*
* Ex:
* <a href="post/1" data-method="delete">destroy</a>
* // Will trigger the route Route::delete('post/(:id)')
*
*/
$(function(){
$('[data-method]').append(function(){
return "\n"+
"<form action='"+$(this).attr('href')+"' method='POST' style='display:none'>\n"+
" <input type='hidden' name='_method' value='"+$(this).attr('data-method')+"'>\n"+
"</form>\n"
})
.removeAttr('href')
.attr('style','cursor:pointer;')
.attr('onclick','$(this).find("form").submit();');
});This way a link created using
{{ HTML::link('thing/1', 'Remove Thing', array( 'data-method' => 'delete')) }}
<!-- or simply -->
<a href="application/thing/1" data-method="delete">Remove Thing</a>Will actually acces the DELETE route when clicked by the user.
100% Restful friendly (:
Last edited by Zizaco (2013-01-19 13:22:19)
Offline
It seems a bug exist in this jquery script, I have to change
$('.[data-model]')....to
$('[data-model="delete"]')....to make it work. Anyway, you really save my day. Thanks for sharing.
Last edited by markwu (2012-11-16 10:17:58)
Mark Wu
http://blog.markplace.net
Offline
Thanks for sharing. It's very usefull ![]()
Laravel is great! Thanks everybody! :)
Offline
It seems a bug exist in this jquery script, I have to change
to
$('[data-model="delete"]')....to make it work. Anyway, you really save my day. Thanks for sharing.
You're right. But try
$('[data-model]').... (without the "dot") instead.
This way you can use it to access PUT routes too. (:
I've fixed the script. Thanks.
Last edited by Zizaco (2012-11-16 11:58:48)
Offline
$('[data-model]')....(without the "dot") instead.
This way you can use it to access PUT routes too. (:I've fixed the script. Thanks.
Wow ... It's great!!! Thanks.
Mark
Mark Wu
http://blog.markplace.net
Offline
I'm hoping Laravel 4's Glow will handle things like that.
Last edited by Anahkiasen (2012-11-16 13:52:29)
Offline
Anahkiasen
I don't have time to look into L4 detail yet. What is "Glow" for?
Mark
Mark Wu
http://blog.markplace.net
Offline
Like it says in the source: JavaScript helper for the Illuminate Framework.
As far as I can tell it's only a URL helper for now, but something like rails.js would be really nice ![]()
We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr
Offline
A guy who named 'k_89' in irc channel give the following example, it is another good implementation for restful. Thanks his sharing.
$(function () {
var makeRequest = function (method, target) {
if(method === 'GET') {
window.location.href = target;
return;
}
var html = '<form action="'+target+'" method="POST">';
html += '<input type="hidden" name="_method" value="'+method+'">';
html += '</form>';
$(html).submit();
};
$('a').on('click', function (ev) {
var $a = $(this);
if($a.data('method') === 'DELETE') {
ev.preventDefault();
var answer = confirm("Are you sure?");
answer && makeRequest($a.data('method'), $a.attr('href'));
}
});
});Original link is http://paste.laravel.com/d9A
Mark
Mark Wu
http://blog.markplace.net
Offline
Thanks Andrew - this is exactly what I was looking for!
Offline
Hi, sorry to do necroposting, but hope some of the OPs are still around. I tried both solutions but to no avail - the page is just reloaded and the controller not called. No errors are thrown either. I am a noob at javascript and maybe I am overlooking something really trivial or maybe the solutions do not work on Laravel 4.
Can anyone suggest how to properly debug that JS snippet / make sure it is at least called?
Thanks
Offline
This was a great help to me! Thanks!!!
Offline
Thanks for sharing. But this way the deletions would be CRSF vulnerables (and also deletions require Javascript). I think it may be better a HTML macro called linkActionDelete() or something like that.
Offline
Here's what I got:
/*
|--------------------------------------------------------------------------
| Delete form macro
|--------------------------------------------------------------------------
|
| This macro creates a form with only a submit button.
| We'll use it to generate forms that will post to a certain url with the DELETE method,
| following REST principles.
|
*/
Form::macro('delete',function($url, $button_label='Delete',$form_parameters = array(),$button_options=array()){
if(empty($form_parameters)){
$form_parameters = array(
'method'=>'DELETE',
'class' =>'delete-form',
'url' =>$url
);
}else{
$form_parameters['url'] = $url;
$form_parameters['method'] = 'DELETE';
};
return Form::open($form_parameters)
. Form::submit($button_label, $button_options)
. Form::close();
});and you call it like this, for example:
{{Form::delete('resource/'. $resource->id, 'Delete')}}or like this:
{{Form::delete('resource/'. $resource->id,
'Delete',
array('id'=>'the_form_id','class' => 'the-delete-form'),
array('class'=>'the-delete-link')
)}}More details on my blog (blog dot elenakolevska dot com), it's the second post.
Offline
Here's what I got:
[snip]
More details on my blog (blog dot elenakolevska dot com), it's the second post.
Thank you Elena! I worked around the problem, but will try your code as soon as needed.
Offline
You're very welcome
Hope it works for you and if it doesn't, you can easily tweak it...
Offline