Skip to content

Instantly share code, notes, and snippets.

@GabrieliRoldao
Created April 12, 2016 20:36
Show Gist options
  • Select an option

  • Save GabrieliRoldao/f00a2a35e9fad4f036e721ffe5a145d9 to your computer and use it in GitHub Desktop.

Select an option

Save GabrieliRoldao/f00a2a35e9fad4f036e721ffe5a145d9 to your computer and use it in GitHub Desktop.
Problema com js
@extends('layouts.admin-master')
@section('styles')
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="{{URL::to('src/css/categories.css')}}" type="text/javascript" />
@endsection
@section('content')
<div class="container">
<section id="category-admin">
<form action="" method="post">
<div class="input-group">
<label for="name">Nome da categoria</label>
<input type="text" name="name" id="name" />
<button class="btn">Criar categoria</button>
</div>
</form>
</section>
<section class="list">
@foreach($categories as $category)
<article>
<div class="category-info" data-id="{{$category->id}}">
<h3>{{ $category->name }}</h3>
</div>
<div class="edit">
<nav>
<ul>
<li class="category-edit"><input type="text" /></li>
<li><a href="#">Editar</a></li>
<li><a href="#">Excluir</a></li>
</ul>
</nav>
</div>
</article>
@endforeach
</section>
</div>
@if($categories->lastPage() > 1)
<section class="paginacao">
@if($categories->currentPage() !== 1)
<a href="{{ $categories->previousPageUrl() }}"><i class="fa fa-caret-left"></i></a>
@endif
@if($categories->currentPage() !== $categories->lastPage())
<a href="{{ $categories->nextPageUrl() }}"><i class="fa fa-caret-right"></i></a>
@endif
</section>
@endif
@endsection
@section('scripts')
<script type="text/javascript">
var token = "{{ Session::token() }}";
</script>
<script type="text/javascript" src="{{URL::asset('src/js/categories.js')}}"></script>
@endsection
var docReady = setInterval(function () {
if (document.readyState !== "complete") {
return;
}
clearInterval(docReady);
document.getElementsByClassName('btn')[0].addEventListener('click', createNewCategory);
}, 100);
function createNewCategory(event) {
event.preventDefault();
var name = event.target.previousElementSibling.value;
if (name.length === 0) {
alert("Por favor, digite um nome para categoria");
return;
}
ajax("POST", "/admin/blog/category/create", "name=" + name, newCategoryCreated, [name]);
}
function newCategoryCreated(params, success, responseObj) {
location.reload();
}
function ajax(method, url, params, callback, callbackParams) {
var http;
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else {
http = new ActiveXObject("Microsoft.XMLHTTP");
}
http.onreadystatechange = function() {
if(http.readyState == XMLHttpRequest.DONE){
if(http.status == 200){
var obj = JSON.parse(http.responseText);
callback(callbackParams, true, obj);
}else if(http.status == 400){
alert('deu merdss');
callback(callbackParams, false);
}else{
var obj = JSON.parse(http.responseText);
if(obj.message){
alert(obj.message);
}else{
alert('deu merdss 2');
}
}
}
};
http.open(method, baseUrl + url, true);
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
http.send(params + "&_token=" + token);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment