Prototype AJAX Updater Multiple Requests - ajax

I am just beginning to learn AJAX and am trying to build a cascading dropdown that pulls information from a database. My AJAX.Updater code works, but not only for one of the dropdowns. How do I make multiple AJAX.Updater calls?
<script type="text/javascript" src="jQuery/jquery-latest.js"></script>
<script type="text/javascript" src="jQuery/prototypejs.js"></script>
<script language="javascript">
jQuery(document).ready(function()
{
jQuery('#regiondropdown').change(function() {
dropdowns(jQuery(this).val(),"foo");
});
jQuery('#foodropdown').change(function() {
dropdowns(jQuery(this).val(),"bar");
});
}
);
function dropdowns(str,type)
{
if (type=="foo") {
new Ajax.Updater('foo', 'foo_dropdown.php', { method: 'get', parameters: {foo: str} });
} else if (type=="bar") {
new Ajax.Updater('bar', 'bar_dropdown.php', { method: 'get', parameters: {bar: str} });
}
}
</script>

I can't see what it is so I suggest you fire up your debugger.
1) Check the value of jQuery(this).val() for both cases. "this" might not be what you believe in Javascript.
2) Check what happens if you switch the if.. and else... in dropdowns. Maybe there is a (, { or ; out of place.
HTH

Related

Angular ng-grid issue

I am trying to use ng-grid using the following tutorial.
http://angular-ui.github.io/ng-grid/
Here is my View
<script type="text/javascript" src="#Url.Content("~/Scripts/jquery-1.8.2.js")"></script>
<script type="text/javascript" src="#Url.Content("~/Scripts/angular.js")"></script>
<script type="text/javascript" src="#Url.Content("~/Scripts/angular-resource.js")"></script>
#*<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>*#
<script type="text/javascript" src="#Url.Content("~/Scripts/ng-grid-2.0.7.min.js")"></script>
<script src="~/Scripts/PostsController.js"></script>
<script src="~/Scripts/postreq.js"></script>
<link rel="stylesheet" type="text/css" href="~/Content/ng-grid.min.css" />
<div data-ng-controller="PostsController">
<div class="gridStyle" data-ng-grid="gridOptions"></div>
</div>
Here is my Angular code
function PostsController($scope, $http) {
$http({ method: 'GET', url: '/api/request' }).
success(function (data, status, headers, config) {
$scope.posts = data;
}).
error(function (data, status, headers, config) {
$scope.error = "An Error has occured while loading posts!";
$scope.loading = false;
});
$scope.gridOptions = { data: 'posts' };
}
However, when i run the application i see blank square in browser and there is no error showing in the console.
Please help me to fix this issue.
FYI: I checked API response directly in browser and it shows with data.
It seems to me that you are missing some steps of the tutorial you are following :
Where you declare your app module, add ngGrid:
angular.module('myApp',['ngGrid']);
I do not see your module declaration anywhere.
Do you have ng-app="myApp" defined at the top of your page?
I also noticed that you do not have a css class defined for "gridStyle". "gridStyle" is not part of the ng-grid css.
Sounds odd, but this worked for me by using a timeout. Try it like this:
setTimeout(function () {
$http.get('/api/request')
.success(function (data, status) {
$scope.posts = data;
})
.error(function (data, status) {
alert(status);
});
}, 100);

Understanding jQuery ' $(function () {...}); ' logic

I have a partial view in ASP.NET MVC 3, with a script:
<script type="text/javascript" >
$(function () {
$("#tagbox").autocomplete({
source: "/Tag/GetTags",
minLength: 1,
select: function (event, ui) {
$("tagbox").val(ui.item.value);
}
});
});
</script>
when I load the patial view in my content div, the auto complete won't work, unless I remove the '$(function () {... }) so the script looks like this:
<script type="text/javascript" >
$("#tagbox").autocomplete({
source: "/Tag/GetTags",
minLength: 1,
select: function (event, ui) {
$("tagbox").val(ui.item.value);
}
});
</script>
But when loading it as new view by accessing the URL, everything works just fine.
Also I have those references on my main view:
<script src="http://static.jsbin.com/js/prod/jsbin-3.4.4.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="../../Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
Second, when I change the order between the references, my ajax call opens as a new view and not as partial. Maybe one of them is unnecessary or something?
Please follow the below link it will clearly explain about the jquery $(function(){}.
$(function(){}

Apply jQuery to elements created by ajax

I'm trying to figure out how to apply jQuery methods, functions etc for elements that have been created with ajax.
In my page there is a dropdown list #cat_id and depending on the selection made, a set of UL elements are created. I then need to call the following function for each element created:
$('#allowSpacesTagsX').tagit({ itemName: 'itemX', fieldName: 'tagsX', availableTags: sampleTagsX, allowSpaces: true });
where allowSpacesTagsX (X=1,2,3,....) is the id of the created UL elements. This method binds the UL to an auto-complete tagging widget similar to the tagging element used in StackOverflow.
Normally the code above would be included in the document.ready for the static elements but I need to add it for each element created with ajax.
This is a small sample of code to understand my question better:
<script src="../js/tag-it.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('#cat_id').live('change', function(e){
$.post('../includes/ajax.php', {
cat_id : $(this).find("option:selected").attr('value')}, function(data) {
$("#tagholder").html(data);
});
});
});
</script>
Edit:
example added to demonstrate an example of the code that should be produced:
$(function(){ // Creates the arrays of tags to be assigned to each tag field
var sampleTags1 = ['USB', 'DVB', 'GSM', 'OEM'];
var sampleTags2 = ['Alfa Romeo', 'Audi', 'Chevrolet', 'Mazda', 'Mercedes'];
var sampleTags3 = ['20cm', '21cm', '8in'];
$('#allowSpacesTags1').tagit({ itemName: 'item1', fieldName: 'tags1', availableTags: sampleTags1, allowSpaces: true });
$('#allowSpacesTags2').tagit({ itemName: 'item2', fieldName: 'tags2', availableTags: sampleTags2, allowSpaces: true });
$('#allowSpacesTags3').tagit({ itemName: 'item3', fieldName: 'tags3', availableTags: sampleTags3, allowSpaces: true });
});
<script src="../js/tag-it.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('#cat_id').live('change', function(e){
$.post('../includes/ajax.php', {
cat_id : $(this).find("option:selected").attr('value')},
function(data) {
$("#tagholder").html(data);//places your ajax content
//grab that element again and then apply any jquery function to it
//example add a class to an arbitary h1 tag inside #tagholder
$("#tagholder h1").css('class','heading');//like so
});
});
});
</script>

Mvc3 validatios are not firing

I am using Javascript serializer for passing model data to controller but this is not firing the code validation.
Here is the code
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/microsoftajax.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#btnCreateDepartment").click(function () {
var name = $('#txtDeptName').val();
$.ajax({
data: { modelName: 'DEPARTMENT', values: Sys.Serialization.JavaScriptSerializer.serialize(
{
DeptName: name
}
)
},
url: '#Url.Action("Create", "Office")',
type: "POST",
complete: function (result) {
debugger
alert("complete");
},
error: function (result) {
debugger
alert(result.statusText);
}
}); //Endof ajax
return false;
}); //end of click
}); // end of ready
</script>
In model the i have added validation as following
[Required(ErrorMessage = "Please Enter department")]
public string DeptName { get; set; }
But this is not getting fired .
Passing the object via ajax to the controller will disable the default mvc validation form. This validation will only work if you do a normal post.
Anyway if you want to use ajax you need to do the validation using jquery. If you decide not to use ajax then you can use mvc's built in validation. I also recommend fluent validation library for MVC
Change data parameter of your jQuery ajax to
data: { DeptName: name },
so it contains only properties of your model. Other department properties should follow DeptName ex. { DeptName: name, DeptAddress: address, ... }.
If your mvc model was mirrored on client side as a javascript model department you would just go:
data: { JSON.stringify(department) },

squeezebox ajax mootools

I use the modal window a fair bit in joomla and just found out that it doesn't work with ajax added links. What is the code I need to have the modal window work with those links?
I'm better at jquery than moo tools ...
Thanks,
Mat
I found this code but can't get it to work.
<script type="text/javascript">
window.addEvent('domready', function() {
SqueezeBox.initialize({
ajaxOptions: {
evalScripts: true
}
});
//SqueezeBox.initialize({});
$$('a.modal').each(function(el) {
el.addEvent('click', function(e) {
new Event(e).stop();
SqueezeBox.fromElement(el);
});
});
});
window.addEvent('domready', function() {
window.addEvent('load', function(){
//alert('clicked!');
SqueezeBox.fromElement($('a.modal'));
});
});
</script>
I use Rokbox (Mootools) from Rockettheme available for free here : http://www.rockettheme.com/extensions-downloads/club/1005-rokbox
A must have :)

Resources