-
-
Save JeffreyWay/5c367388542e7c2ca421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vue.directive('ajax', { | |
params: ['complete'], | |
bind: function () { | |
this.el.addEventListener( | |
'submit', this.onSubmit.bind(this) | |
); | |
}, | |
onSubmit: function (e) { | |
var requestType = this.getRequestType(); | |
this.vm | |
.$http[requestType](this.el.action) | |
.then(this.onComplete.bind(this)) | |
.catch(this.onError.bind(this)); | |
e.preventDefault(); | |
}, | |
onComplete: function () { | |
if (this.params.complete) { | |
alert(this.params.complete); // Use pretty flash message instead. | |
} | |
}, | |
onError: function (response) { | |
alert(response.data.message); // Use pretty flash message instead. | |
}, | |
getRequestType: function () { | |
var method = this.el.querySelector('input[name="_method"]'); | |
return (method ? method.value : this.el.method).toLowerCase(); | |
}, | |
}); | |
new Vue({ | |
el: 'body', | |
http: { | |
headers: { | |
// You could also store your token in a global object, | |
// and reference it here. APP.token | |
'X-CSRF-TOKEN': document.querySelector('input[name="_token"]').value | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST" | |
action="/posts/3" | |
v-ajax complete="Okay, the post has been deleted." | |
> | |
{{ method_field('DELETE') }} | |
{{ csrf_field() }} | |
<button type="submit">Delete Post</button> | |
</form> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.15/vue.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/0.6.1/vue-resource.min.js"></script> | |
<script src="/js/app.js"></script> |
You are right @socieboy no id is needed after all.
Thanks for the code. Food for thought: I'd use this merely as a fallback solution. After all, if we use Vue, there should be some kind of data model and thus, we could directly work with it via this.vm.$data
. So basically like this:
this.vm.$http[requestType](this.el.action, this.vm.$data)... // add callbacks
@socieboy: Don't you have to somehow bind (this) to getFormData() ? E.g.,
.$http[requestType](this.el.action, this.getFormData()).bind(this)
Otherwise the console squawks that serializedData is undefined.
Hello guys, how would you tackle showing validation errors?
I was thinking something like this but not sure how to make it work.
onError: function (response) {
$.each(response.data, function(key, value){
$this.el.querySelector('input[name="'+key+'"]').addClass('input-error-class');
});
}
i get
Uncaught TypeError: Cannot read property 'delete' of undefined
any ideea why ?
solved.
i forget about Vue.use(VueResource);
if you are ok to use formData
you could simply use this
var formData = new FormData(this.el)
this.vm.$http[this.getRequestType()](this.el.action, formData)
//--------
Hell,
Is any update of this gist for vue-2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figure out pretty much as you have done but without use the ID in the form tag.