Skip to content

Commit

Permalink
fix(ajax): elgg/Ajax view() and form() set $vars as expected
Browse files Browse the repository at this point in the history
The `elgg/Ajax` module was auto-converting any request with `options.data`
to use the `POST` method. This makes sense for things like actions, which
may send a large amount of form data, but it caused `$vars` to not be
populated as expected because only `GET` params are injected.

If you specified `options.data` with these methods before, note the server
will no longer receive this data as `$_POST` on the server.

Fixes #10667
  • Loading branch information
mrclay committed Dec 24, 2016
1 parent 11c4640 commit abf8a9c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
31 changes: 20 additions & 11 deletions js/tests/ElggAjaxTest.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,25 +115,34 @@ define(function(require) {
expect(ajax._ajax_options.method).toEqual('POST'); expect(ajax._ajax_options.method).toEqual('POST');
}); });


$.each(['path', 'form', 'view'], function (i, method) { it("path(): non-empty object data changes default to POST", function() {
ajax.path('foo', {
data: {bar: 'bar'}
});
expect(ajax._ajax_options.method).toEqual('POST');
});


it(method + "() defaults to GET", function() { it("path(): non-empty string data changes default to POST", function() {
ajax[method]('foo'); ajax.path('foo', {
expect(ajax._ajax_options.method).toEqual('GET'); data: '?bar=bar'
}); });
expect(ajax._ajax_options.method).toEqual('POST');
});


it(method + "(): non-empty object data changes default to POST", function() { $.each(['form', 'view'], function (i, method) {
it(method + "(): non-empty object data left as GET", function() {
ajax[method]('foo', { ajax[method]('foo', {
data: {bar: 'bar'} data: {bar: 'bar'}
}); });
expect(ajax._ajax_options.method).toEqual('POST'); expect(ajax._ajax_options.method).toEqual('GET');
}); });
});


it(method + "(): non-empty string data changes default to POST", function() { $.each(['path', 'form', 'view'], function (i, method) {
ajax[method]('foo', {
data: '?bar=bar' it(method + "() defaults to GET", function() {
}); ajax[method]('foo');
expect(ajax._ajax_options.method).toEqual('POST'); expect(ajax._ajax_options.method).toEqual('GET');
}); });


it(method + "(): empty string data leaves default as GET", function() { it(method + "(): empty string data leaves default as GET", function() {
Expand Down
1 change: 1 addition & 0 deletions mod/developers/start.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function developers_init() {
elgg_register_page_handler('developers_ajax_demo', 'developers_ajax_demo_controller'); elgg_register_page_handler('developers_ajax_demo', 'developers_ajax_demo_controller');


elgg_register_external_view('developers/ajax'); // for lightbox in sandbox elgg_register_external_view('developers/ajax'); // for lightbox in sandbox
elgg_register_ajax_view('developers/ajax_demo.html');
$sandbox_css = elgg_get_simplecache_url('theme_sandbox.css'); $sandbox_css = elgg_get_simplecache_url('theme_sandbox.css');
elgg_register_css('dev.theme_sandbox', $sandbox_css); elgg_register_css('dev.theme_sandbox', $sandbox_css);


Expand Down
1 change: 0 additions & 1 deletion mod/developers/views/default/developers/ajax_demo.html

This file was deleted.

7 changes: 7 additions & 0 deletions mod/developers/views/default/developers/ajax_demo.html.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

if (!isset($vars['entity']) || !$vars['entity'] instanceof ElggSite) {
register_error('$vars not set by ajax.form()');
}

?><p>view demo</p>
8 changes: 6 additions & 2 deletions mod/developers/views/default/developers/ajax_demo.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ define(function(require) {
if (html_page.indexOf('path demo') != -1) { if (html_page.indexOf('path demo') != -1) {
log("PASS path()"); log("PASS path()");


return ajax.view('developers/ajax_demo.html'); return ajax.view('developers/ajax_demo.html', {
data: {guid: 1}
});
} }
}) })
.then(function (div) { .then(function (div) {
if (div.indexOf('view demo') != -1) { if (div.indexOf('view demo') != -1) {
log("PASS view()"); log("PASS view()");


return ajax.form('developers/ajax_demo'); return ajax.form('developers/ajax_demo', {
data: {guid: 1}
});
} }
}) })
.then(function (form) { .then(function (form) {
Expand Down
4 changes: 4 additions & 0 deletions mod/developers/views/default/forms/developers/ajax_demo.php
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php <?php
// This will be fetched via ajax by the developers/ajax_example AMD module // This will be fetched via ajax by the developers/ajax_example AMD module


if (!isset($vars['entity']) || !$vars['entity'] instanceof ElggSite) {
register_error('$vars not set by ajax.view()');
}

echo "form demo"; echo "form demo";
2 changes: 2 additions & 0 deletions views/default/elgg/Ajax.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ define(function (require) {


options = options || {}; options = options || {};
options.url = 'ajax/view/' + view; options.url = 'ajax/view/' + view;
options.method = options.method || 'GET';


// remove query // remove query
view = view.replace(query_pattern, '').replace(slashes_pattern, ''); view = view.replace(query_pattern, '').replace(slashes_pattern, '');
Expand Down Expand Up @@ -274,6 +275,7 @@ define(function (require) {


options = options || {}; options = options || {};
options.url = 'ajax/form/' + action; options.url = 'ajax/form/' + action;
options.method = options.method || 'GET';


// remove query // remove query
action = action.replace(query_pattern, '').replace(slashes_pattern, ''); action = action.replace(query_pattern, '').replace(slashes_pattern, '');
Expand Down

0 comments on commit abf8a9c

Please sign in to comment.