Last active
August 29, 2015 14:13
-
-
Save ryaan-anthony/01c56cd1fc794f38b561 to your computer and use it in GitHub Desktop.
Simple Ajax wrapper for Magento/Prototype.js
This file contains hidden or 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
| var AjaxBackend = Class.create(); | |
| AjaxBackend.prototype = { | |
| initialize: function(url) { | |
| this.url = url; | |
| }, | |
| submit: function(){ | |
| this.request({ | |
| // build request here | |
| }); | |
| }, | |
| request: function(params){ | |
| new Ajax.Request(this.url, { | |
| method: 'post', | |
| parameters: params, | |
| onSuccess: this.finished | |
| }); | |
| }, | |
| finished: function(response){ | |
| // handle response here | |
| } | |
| } |
This file contains hidden or 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
| <input class="input-text" type="text" id="foo" /> | |
| <input class="input-text" type="text" id="bar" /> | |
| <button type="button" onclick="ajaxExample.submit()" class="button"> | |
| <span><span>Submit</span></span> | |
| </button> | |
| <script type="text/javascript"> | |
| //<![CDATA[ | |
| var ajaxExample = new AjaxBackend('<?php echo Mage::getUrl('example/controller/action') ?>'); | |
| ajaxExample.submit = function(){ | |
| this.request({ | |
| foo: $('foo').value, | |
| bar: $('bar').value | |
| }); | |
| }; | |
| ajaxExample.finished = function(response){ | |
| console.log(response.responseText); | |
| }; | |
| //]]> | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment