-
Notifications
You must be signed in to change notification settings - Fork 49
Ajax CRUD
JP Barbosa edited this page Mar 20, 2016
·
4 revisions
bower install jquery --save
bower install jquery-ujs --save
nano resources/views/layouts/ajax.blade.php
@yield('content')
nano resources/views/shared/modal.blade.php
<div class="modal" id="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body"></div>
</div>
</div>
</div>
nano resources/views/layouts/html.blade.php
...
@include('shared.modal')
<script type="text/javascript" src='/js/3rd-party.js'></script>
<script type="text/javascript" src='/js/app.js'></script>
</body>
</html>
nano app/Providers/AppServiceProvider.php
...
use Illuminate\Support\ServiceProvider;
use Request;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('*', function ($view) {
$remote = Request::ajax() ? true : null;
$layout = $remote ? 'layouts.ajax' : 'layouts.html';
$view->with(compact('remote', 'layout'));
});
}
...
nano resources/views/articles/index.blade.php
...
<h1>Articles</h1>
{!! link_to_route('articles.create', 'New Article', null, [
'class' => 'btn btn-primary btn-lg',
'data-remote' => 'true' ]) !!}
...
nano resources/views/articles/create.blade.php
{!! Form::open(['route' => 'articles.store', 'data-remote' => $remote]) !!}
mkdir resources/assets/js
nano resources/assets/js/loadRemoteContent.js
$(document).on('ajax:success', '.btn[data-remote]', function(e, data, status, xhr) {
var target = $('#modal');
target.find('.modal-body').html(xhr.responseText);
target.modal('show');
target.on('ajax:success', 'form[data-remote]', function(e, data, status, xhr) {
$('#content').html(xhr.responseText);
target.modal('hide');
});
target.on('ajax:error', 'form[data-remote]', function(e, data, status, xhr) {
target.find('#alert-box')
.show()
.find('ul')
.html(toList(data.responseJSON));
});
});
function toList(messages) {
var converted = '';
$.each(messages, function(key, value)
{
converted += '<li>' + value + '</li>';
});
return converted;
}
nano gulpfile.js
...
elixir(function(mix) {
...
mix.scriptsIn('', 'public/js/app.js');
});
gulp
php artisan serve
open http://localhost:8000/articles
git add .
git commit -m "Add ajax CRUD"
Next step: Send Email
- Setup
- Basic CRUD
- Validation
- Views
- Association
- Association Controller
- Association Views
- Basic Template
- Bootstrap
- Bootstrap CRUD
- Alerts
- Welcome Page
- Ajax CRUD
- Send Email
- Send Email Views
- Jobs Queue
- Captcha
- Async External Content
- Cached External Content
- Tests Setup
- Functional Tests
- Acceptance Tests
- Continuous Integration
- Deploy with Heroku
- Deploy with Forge
- Update README