Zend Framework :: Ajax Requests - ajax

I am looking out for any library that would facilitate Ajax in Zend (if any exist). Also can somebody throw some light on the built-in Ajax support that comes with ZF.
I tried googling but was not satisfied with the results.
-DevD

altough JQuery is well integrated with the Zend Framework. There is an libray inside the /extras Folder.
There are helpers for AjaxRequests, different View Widgets, and for loading the Library from Google/AOL CDN.
For more information i would suggest to visit the Zend Framework Documentation for :
ZendX Jquery

Dojo is shipped with the Zend Framework and they facilitate ajax style calls.
If you have a look at Dojo ToolKit to find out more about the what that framework can provide you, these include the ajax calls (search for xhrGet and xhrPost)
Also if you look here in the framework documentation to help you use dojo in your Zend Framework Project. Zend_Dojo
Hope this helps

If you have any expertise in YUI JS framework, it is really easy to listen to the events and make Ajax calls and to collect your elements through Selector query and then apply CSS rules on them (if that is what you want). You can have a look at this tutorial to understand more about Zend-YUI relationship
http://ciitronian.com/blog/programming/javascript/creating-ajax-based-form-zend-framework-yui/

Take a look at this Zend Framwork 2 module.
If you do not have a application you can use the Wasabilib Skeleton https://github.com/WasabiLib/wasabilib_zf2_skeleton_application. It comes with all necessary assets in the right place.
If you already have an application you should only clone the module not the full skeleton.
Minimal requirements: jQuery, ZF2
Add the module to application.config.php.
Include the wasabilib.min.js after jquery in the head of your layout.phtml
How it works
in your .phtml-file you have a form like this:
<form id="simpleForm" class="ajax_element" action="simpleFormExample" method="POST">
<input type="text" name="written_text">
<input type="submit" value="try it">
</form>
Anywhere else in your phtml you can place an element where the response is shown.
In your Controller the following method:
public function simpleFormExampleAction(){
$postArray = $this->getRequest()->getPost();
$input = $postArray['written_text'];
$response = new Response(new InnerHtml("#element_simple_form","Server Response: ".$input));
return $this->getResponse()->setContent($response);
}
The form has a class "ajax_element" this will say the the library that the request will be done with an xmlhttp-request. It wont work if you do not give an id to the requesting element. So the form has the ID "simpleForm". The action is the "path/to/controller" just like a normal request.
In the controller action a new WasabiLib\Ajax\Response object is instanciated.
The InnerHtml class is for replace, prepend and append html or normal text to a selector.
In this case the selector is an ID "element_simple_form". The first parameter of the InnerHtml class is the selector. Make sure that you write #yourElementId or .yourClassSelector. For IDs an "#" and for class selectors "."
The second parameter is the Text you want to fill in this element.
The response object can handle a lot more responses which you can add with
$response->add($anotherResponseType);
A list of possible response types is on its homepage wasabilib.org
The module is build to handle ajax request an responses in a very simple way. Once you have understood the behavior you can handle almost every practical ajax need.

Related

Can a view component call its own methods via ajax after rendering within the parent page?

Maybe I am missing the point, but if you have a ViewComponent, the examples ive seen so far, all do their work within their 'InvokeAsync method, where they are passed a model and return a view.
If the view contains a databound control and you need to bind to data via Ajax, where can those methods be, within the ViewComponent or the parent page?
Ok, so take this example
https://github.com/pkellner/progress-telerik-blog-viewcomponent/tree/master/WebApp/Pages/Components/RatingControl
Can the viewcomponent be used for helper methods that are called from its own view, eg with Ajax loading. Same question goes for other controls as well, eg a DataGrid within a viewcomponent, where would the variosu crud helper methods go?
Well, it finally dawned on me that NO you can not do what was suggesting. Per the docs, a ViewComponent (as least as of 3.0) do not respond directly as an http endpoint.
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-3.0
Solution - make an http endpoint, eg. a web api, which could be called via jquery/ajax from the markup in the viewcomponent.

Add pagelink via ajax in tapestry

After an ajax call, I want to inject some element (list) in page. For each element of the list, I want to attach t:pagelink. I use PrototypeJs as js framework. Have you any idea?
I have already test the code below but doesn't work (it not render the t:pagelink)
new Ajax.Request('my_service_url', {
onSuccess: function(response) {
response.responseJSON.data.each(function(item){
var li = '<li>'+
'<span>'+item.title+'</span>'+
'<t:pagelink page="examples/navigation/PageLinks2">'+item.link+'</t:pagelink>'+
'</li>';
$('mylist').insert(li);
});
}
});
Thanks for your reply but I've found the solution. Just use processReply and it's ok. Indeed, the response needed a bit modification.
This does not work with Tapestry, as Lance Java pointed out, because the Javascript snippet is executed on the client. The client can't submit the <t:pagelink> tag to Tapestry for interpretation.
What you need to do instead is use the Ajax semantic of Tapestry, in particular the Zone component. <t:zone>...</t:zone> defines a section of your HTML document which will be replaced with a new version, rendered from .tml to html by Tapestry.
So in your <t:zone>, add <t:pagelink>'s as needed, probably dynamically, based on some condition or in some loop (i.e. with <t:if> or <t:loop> tags). Then, use some mechanism to send an AJAX event back to Tapestry. One simple option would be to use <t:actionlink zone="[ID of your zone]">.
See here for more info on Zones.

JSF Ajax - trigger on partially completed field

I want to implement a "typeahead" type of functionality but for effiency reasons to avoid return a list of possibly thousands of entries, I only want to fire the request to the server when the user has entered at least three characters. I.E. on the 3rd keypress, I want to call my server side search via ajax.
I'm not looking for a full runnable example, just a sketch of how this might be possible, as I'm a bit stumped by it.
I do have a generic ajax handler js file in my app to render the ajax "spinner" so I thought I might be able to hook into event method for status="begin" and somehow abort the request if the input field has less than 3 characters but I don't see how that's possible.
I'm hoping a certain JSF guru might be able to point me in the right direction :)
I'm using standard reference JSF2, no 3rd party libraries...
How about adding a onkeyup="myFunction(event)" to your input
<input type="text" onkeyup="myFunction(event)">
and in js add the following
function myFunction(e){
if (e.target.value && e.target.value.length > 2) {
alert('do some ajax with the value: ' + e.target.value);
}
}
In jsf you can add some hidden input with f:ajax and trigger it from js with somethign like this document.getElementById("myButtonId").click();
Online example

yii Ajax link not working

I put a Ajax link using the following code:
echo chtml::ajaxLink('GO', 'http://localhost/index.php?r=user/delete', array('method'=>'POST'));
But, regardless of giving the second parameter as URL i,e 'http://localhost/index.php?r=user/delete'. It generates link with the current URL in the browser not the URL I just specified.
What is the issue? How could I create AJAX link? Google several hours but can't solve the issue.
Any kind of help is highly appreciated.
First of all, you should always try and create normalized urls.
But i think your doubt lies in the # that is generated/appended. If you go and check the source of yii ajaxLink you'll see this:
public static function ajaxLink($text,$url,$ajaxOptions=array(),$htmlOptions=array())
{
if(!isset($htmlOptions['href']))
$htmlOptions['href']='#';
$ajaxOptions['url']=$url;
$htmlOptions['ajax']=$ajaxOptions;
self::clientChange('click',$htmlOptions);
return self::tag('a',$htmlOptions,$text);
}
so if you don't set the href property of the a tag in the htmloptions array, the # will be appended.
You should also understand that yii uses jquery, so if you check out the source of the page, you'll see at the bottom, how jquery is used to carry out an ajax request, your actual url that is called will also be seen in that script. So the third option/parameter in ajaxLink is for options for jquery's ajax function. You can create better ajax links using this option.
Regardless of where(which controller) your url points to in your project, the action associated with that url will be called.
So anyway, you can modify your code like this if you want the url to be shown and not a # :
echo CHtml::ajaxLink('GO', 'http://localhost/index.php?r=user/delete',
array('type'=>POST), //there are various other options for jquery ajax
array('href'=>'http://localhost/index.php?r=user/delete'));
To make better ajax links i would suggest going through jquery's ajax documentation. There is an option for a success function, that you can use to let the user know that the operation was completed.
Hope this helps, don't hesitate to leave comments if i haven't answered your question completely.
Have you tried:
echo CHtml::ajaxLink('GO', array('/user/delete'), array('method'=>'POST'));
as the ajaxLink documentation suggests...? Look also at the normalizeUrl method.
Using these methods, which in turn are using createUrl, is usually better since it will take care to create a valid url for your site.
I had the same issue(or maybe similar).
I've used renderPartial to load view and later in that view i was using ajaxLink and it was not working.
What i have found, that when using renderPartial, there was no jquery script for ajax action.
What you have to do is to add 4th argument(true) in renderPartial function to generate jquery script.
See the documentation: http://www.yiiframework.com/doc/api/1.1/CController/#renderPartial-detail
Hope it helps and saves time to figure it out.

update the row using ajax in cakephp

i am using cakephp in my project. in this at one section i need to update the particular row onclick of the image. using ajax. i used mootools as javascript library. so please help me how could i do this.
thanks in advance
Simply speaking:
Create a CakePHP controller action that performs the row update.
Determine the URL of the controller action you just created. (ie. /controllername/actionname)
Determine if you need to do a GET or POST request to this URL for it to work.
Put code in your view that attaches an "onclick" event that makes and AJAX (GET/POST) request to the above controller.
CakePHP has a javascript helper that traditionally produced Prototype code, but in v1.3 it is now able to produce code for other Javascript frameworks (such as Mootools, jQuery, etc.)
However, many suggest writing your javascript in javascript (eg. actually using the Mootools framwork), rather than writing your javascript in PHP (like using CakePHP's helper to produce Mootools code).
Either way, in your view you need to have something like: <?php echo $js->link(.. or <script>Moo.. or <a onclick="Moo.. to attach your Javascript to that link.
You may also wish for your controller action to return some sort of response indicating whether or not the row update failed or succeeded. In that case you need to make sure the CakePHP controller action you are calling has a view that outputs this. JSON seems to be the ideal format for this (eg. { success: true }), but you need to remember to turn off Cake's debug output. This response can be captured into a variable by your Mootools code where you can decide what to do with it (eg. displaying an error).
As i know most programmer work with protype.js library.
i am giving you link see
go to there

Resources