From 1b5285ee2c31ec8afbb09b3a6e5c048d7976632d Mon Sep 17 00:00:00 2001 From: Dan Hixon Date: Mon, 7 Jun 2010 13:42:29 -0700 Subject: [PATCH] allow form values to be passed in a link using data-remote attribute as well as ability to over-ride href attribute with data-url. --- src/rails.js | 4 +++- test/public/test/call-remote.js | 22 ++++++++++++++++++++++ test/public/test/data-remote.js | 31 ++++++++++++++++++++++++++++--- test/public/test/settings.js | 7 +++++++ 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/rails.js b/src/rails.js index 4e9a8aef..cb03da97 100644 --- a/src/rails.js +++ b/src/rails.js @@ -24,7 +24,7 @@ jQuery(function ($) { callRemote: function () { var el = this, method = el.attr('method') || el.attr('data-method') || 'GET', - url = el.attr('action') || el.attr('href'), + url = el.attr('action') || el.attr('data-url') || el.attr('href'), dataType = el.attr('data-type') || 'script'; if (url === undefined) { @@ -32,6 +32,8 @@ jQuery(function ($) { } else { if (el.triggerAndReturn('ajax:before')) { var data = el.is('form') ? el.serializeArray() : []; + data = el.is('a[data-remote]') ? $.parseJSON(el.attr('data-remote')) : data; + $.ajax({ url: url, data: data, diff --git a/test/public/test/call-remote.js b/test/public/test/call-remote.js index 3e707527..003916ad 100644 --- a/test/public/test/call-remote.js +++ b/test/public/test/call-remote.js @@ -76,6 +76,7 @@ test('default method GET should be picked up if no method or data-method is supp $('form[data-remote]') .live('ajax:success', function(e, data, status, xhr) { App.assert_callback_invoked('ajax:success'); + var request_env = $.parseJSON(data)['request_env']; App.assert_get_request(request_env); @@ -144,6 +145,27 @@ test('url should be picked up from href if no action is provided', function() { .trigger('submit'); }); +test('url should be picked up from data-url when href is provided but no action', function() { + expect(2); + + App.build_form({ + 'href': 'http://example.org', + 'data-url': App.url('show') + }); + stop(App.ajax_timeout); + + + $('form[data-remote]') + .live('ajax:success', function(e, data, status, xhr) { + App.assert_callback_invoked('ajax:success'); + var request_env = $.parseJSON(data)['request_env']; + App.assert_request_path(request_env, '/show'); + + start(); + }) + .trigger('submit'); +}); + test('exception should be thrown if both action and url are missing', function() { expect(1); var exception_was_raised = false; diff --git a/test/public/test/data-remote.js b/test/public/test/data-remote.js index cc64c8fa..a942db1d 100644 --- a/test/public/test/data-remote.js +++ b/test/public/test/data-remote.js @@ -8,7 +8,15 @@ module('data-remote', { 'data-remote': 'true', text: 'my address' })); - + + $('#fixtures').append($('', { + 'class': 'json', + href: App.url('update'), + 'data-remote': '{"color": "red"}', + 'data-method': 'POST', + text: 'my address' + })); + $('#fixtures').append($('', { href: App.url('show'), 'data-remote': 'true', @@ -40,7 +48,7 @@ test('clicking on a link with data-remote attribute', function() { expect(3); stop(App.ajax_timeout); - $('a[data-remote]') + $('a[data-remote]:not([data-method])') .live('ajax:success', function(e, data, status, xhr) { App.assert_callback_invoked('ajax:success'); var request_env = $.parseJSON(data)['request_env']; @@ -52,6 +60,24 @@ test('clicking on a link with data-remote attribute', function() { .trigger('click'); }); +test('clicking on a link with put data-method and json in data-remote attribute', function() { + expect(4); + stop(App.ajax_timeout); + console.log($('a[data-remote].json').attr('href')); + $('a[data-remote].json') + .live('ajax:success', function(e, data, status, xhr) { + App.assert_callback_invoked('ajax:success'); + var request_env = $.parseJSON(data)['request_env']; + console.log(request_env); + App.assert_form_vars(request_env, 'color=red') + App.assert_request_path(request_env, '/update'); + App.assert_post_request(request_env); + + start(); + }) + .trigger('click'); +}); + test('clicking on Submit input tag with data-remote attribute', function() { expect(3); stop(App.ajax_timeout); @@ -61,7 +87,6 @@ test('clicking on Submit input tag with data-remote attribute', function() { App.assert_callback_invoked('ajax:success'); var request_env = $.parseJSON(data)['request_env']; - App.assert_request_path(request_env, '/show'); App.assert_get_request(request_env); diff --git a/test/public/test/settings.js b/test/public/test/settings.js index ed78062e..fd886843 100644 --- a/test/public/test/settings.js +++ b/test/public/test/settings.js @@ -42,6 +42,13 @@ App.assert_post_request = function(request_env){ equals(request_env['REQUEST_METHOD'], 'POST', 'request type should be POST'); }; +App.assert_query_string = function(request_env, expected){ + equals(request_env['QUERY_STRING'], expected, 'expected:' + expected + ' but got: ' + request_env['QUERY_STRING']) +} +App.assert_form_vars = function(request_env, expected){ + equals(request_env['rack.request.form_vars'], expected, 'expected:' + expected + ' but got: ' + request_env['rack.request.form_vars']) +} + App.assert_request_path = function(request_env, path) { equals(request_env['REQUEST_PATH'], path, 'request should be sent to right url'); };