Created
April 27, 2011 14:45
-
-
Save troufster/944368 to your computer and use it in GitHub Desktop.
"Proper" handling of MVC3 Ajax links
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
(function($) { | |
/** | |
* Ajaxify MVC3 ActionLinks | |
* | |
* @params { | |
* *success : result handling function(data, textStatus, jqXHR) | |
* *data : Data to send (accepts json data as default) | |
* verb : Http verb to use ('POST', 'GET', ..) Defaults to 'POST' | |
* type : dataType expected from server, Defaults to 'html' | |
* contentType : contentType sent TO server Defaults to 'application/json' | |
* error : error handling function(jqXHR, textStatus, errorThrown) | |
* } | |
* | |
* | |
* Usage: | |
* $('#ajaxstuff').ajaxLink({ | |
* data: { foo: 'Fooinator' }, | |
* success: function (data) { console.log(data); } | |
* }); | |
* | |
*/ | |
$.fn.ajaxLink = function (params) { | |
if (!params || !params.data || !params.success) | |
throw new Error('ajaxLink parameters missing'); | |
//this is equal to $(this) in the current scope | |
//save this reference for use inside nested scopes | |
var link = this, | |
//Defaults to type:html, verb:POST, | |
//content: 'application/json' | |
verb = params.verb || 'POST', | |
type = params.type || 'html', | |
contentType = params.contentType || 'application/json', | |
//If contentType is set, do not assume json | |
data = (params.contentType ? params.data : JSON.stringify(params.data)); | |
//Attach eventhandler to (this) link | |
link.click(function () { | |
$.ajax({ | |
url: params.url || link.attr('href'), | |
type: verb, | |
data: data, | |
contentType: contentType, | |
dataType: type, | |
success: function (result, status, xhr) { | |
params.success(result, status, xhr); | |
}, | |
error: function (xhr, status, error) { | |
if (params.error) params.error(xhr, status, error); | |
} | |
}); | |
//Stop the click from actually posting the page | |
return false; | |
}); | |
}; | |
})(jQuery); |
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
@Html.ActionLink("Click me!", "AjaxStuff", null, | |
new { id = "ajaxstuff" }) |
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
@Ajax.ActionLink("Hurf", "Durf", "Controllurf", | |
new { param = somestuff }, | |
new AjaxOptions { | |
HttpMethod = "Post", | |
OnSuccess = "Somejs" | |
}) |
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
public ActionResult AjaxStuff(object foo) { | |
var stuff = new { Name = "Stuff" + foo }; | |
ViewBag.Stuff = stuff.Name; | |
return View(); | |
} |
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
$('#ajaxstuff').ajaxLink({ | |
data: { foo: 'Fooinator' }, | |
success: function (data) { | |
$('#ajaxdiv').html(data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment