I've managed to successfully add AJAX into a Joomla module through the following kind of process
$( "#addSomething" ).click(function() {
$.ajax({
type: 'POST',
url: "<?php echo JUri::current() . '?task=addSomething'; ?>",
success:function(data){
//On Success Be awesome
},
error:function(){
//Display the error message
}
});
});
$input = new JInput();
$task = $input->get('task', null, 'cmd');
My problem I've got is returning errors in that when I'm throwing errors to date in php I've just been throwing a JApplication message e.g.
JFactory::getApplication()->enqueueMessage('Some error message', 'error');
However this doesn't give the AJAX any sniff of an error so any time I submit it it calls it a 'success'. Hence my problem without having a component style JSON response available.
Too many users don't have Javascript enabled for me to feel happy to just add in a straight 400 header redirect (and in any case I would like the error message to be displayed to the user). I would still like the JApplication to just enqueue an error message through PHP as a fallback when javascript is disabled.
So what would be the best way of going about throwing an error message for the AJAX and then retrieve it so that I can display it to the User (yet still retain a nice non-javascript fallback?) Is there something in the platform I've missed (note I need it to be 2.5 and 3.x compatible if at all possible).
Cheers in advance for any ideas :)
George
From what I understand, the "error:" block only get's executed if there was an error with the request, not if the request was successful and resulted in a server side error (as in, couldn't save, or something like that) http://api.jquery.com/jQuery.ajax
So, what I do to work around this is actually never use the error block (although I should provide a fallback error just in case the request fails), and do all the parsing in my success block.
I typically send a standard JSON object back as the response. If successful, it's structured like this:
{success: true,msg: 'It Worked!',items: [] // if items are involved or whatever}Or if unsuccessful, like this:{success: false,msg: 'AARRRRGGHHH!!!'}Then, my success block is structured like this:success: function(data) {if (data.success) {// do stuff}}Hope that helps.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
Maybe it's time to add a com_ajax with easy ways to hook in to a method to accomplish those common tasks.
--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/9lGEsozYgnA/unsubscribe?hl=en-GB.
To unsubscribe from this group and all of its topics, send an email to joomla-dev-gene...@googlegroups.com.
You might like to look at what the Web Services Working Group are up to.
http://docs.joomla.org/Web_Services_Working_Group
Chris
On Sat, May 18, 2013 at 7:58 AM, Jurian Even <in...@twentronix.com> wrote:
>> Or you could just have the url for your ajax request go directly to the mod_whatever.php and make the request that way.
This is not recommended since you'll open an other entry point and possible security hole. The component and plugin approaches are valid methods.
I certainly can't disagree. But, how could this be achieved otherwise, without com_ajax, if there wasn't a substantial change made to the CMS? My personal goal is to have something working in days, not months or years.
I wholeheartedly agree that UPM and/or Web Services can adress this better architectually, but this might be a baby step in that direction. If anything, it can allow for a single, shared entry point and development of a plugin type for easier implementation of ajax.
Best,
Matt Thomas
Founder betweenbrain™
Lead Developer Construct Template Development Framework
Phone: 203.632.9322
Twitter: @betweenbrain
Github: https://github.com/betweenbrain
Sent from mobile. Please excuse any typos and brevity.
=)
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.
we don't need component for request plugin ajax. we can use a system plugin onAfterRouter for dispatch our own plugin type - https://twitter.com/juliopontes/status/335548885579726848
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
Now, a Component does not have to have a visible interface to it. In fact, it could be as simple as the entry point file, no MVC. The trick will be - what is it supposed to do once it gets there? The more reuse you can get by executing existing functions, the better. But, you'll have to figure out how to map the request to existing functionality. That might be a good use of plugins. (And a plugin can render a Module, in George's case). If the URL has the name of the plugin - the components job may be as simple as firing the plugin and it's done.
George,
In your specific case, you wouldn't need a plugin and component, as your specialized ajax component could have all of the necessary logic needed.
The plugin is related to using a generic, core ajax interface (which doesn't exist yet). My apologies if that confused anything. I do have a working prototype for that, and will start a new thread once the code is in a sufficient state to where it's worth discussing. It is on GitHub if you'd like to take a look at it, or even fork it for your use.
Best,
Matt Thomas
Founder betweenbrain™
Lead Developer Construct Template Development Framework
Phone: 203.632.9322
Twitter: @betweenbrain
Github: https://github.com/betweenbrain
Composed and delivered courtesy of Nexus 7.
--
Now, a Component does not have to have a visible interface to it. In fact, it could be as simple as the entry point file, no MVC. The trick will be - what is it supposed to do once it gets there? The more reuse you can get by executing existing functions, the better. But, you'll have to figure out how to map the request to existing functionality. That might be a good use of plugins. (And a plugin can render a Module, in George's case). If the URL has the name of the plugin - the components job may be as simple as firing the plugin and it's done.So the best scenario is that I need a 'invisi' component to call a plugin to do the AJAX to render my module? Because that seems pretty horrific (in terms of package sizes/loading times if nothing else)
define([ "backbone" ],function(Backbone) {"use strict";// To define the Language accessvar Language = Backbone.Model.extend({url : function() {return base + '?task=setup.setlanguage&format=json';}});return Language;});
// Get the posted values from the request and validate them.$data = $inputJson->get('jform', array(), 'array');$return = $model->validate($data, 'preinstall');...// Redirect to the page.$r->view = $this->input->getWord('view', 'site');$this->sendResponse($r);
var processSiteResponse = function(r) {var messages = r.get('messages'),lang = jQuery('html').attr('lang'),view = r.get('data').view;Joomla.replaceTokens(r.get('token'));if (messages) {Joomla.renderMessages(messages);}if (lang.toLowerCase() === r.get('lang').toLowerCase()) {return true;}else {window.location = base + '?view=' + view;}};...Regards,Anibal