Full pass over all the JSON encoding and JS dialog code. We now abide

by the following rules:

1) An initial dialog or panel load can take either HTML or JSON, but
   the mime type must accurately reflect its payload.

2) dialog form submits can handle a pure HTML response, but the mime
   type must also be correct.  This properly resolves the problem
   where the reauth code gets a JSON response first from the reauth
   code, and then an HTML response when you reauth and continue on to
   a given form -- try it out with Admin > Settings > Advanced.

3) All JSON replies must set the mime type correctly.  The json::reply
   convenience function does this for us.

4) By default, any HTML content sent back in the JSON response should be
   in the "html" field, no longer the "form" field.

The combination of these allows us to stop doing boilerplate code like
this in our controllers:

  // Print our view, JSON encoded
  json::reply(array("form" => (string) $view));

instead, controllers can just return HTML, eg:

  // Print our view
  print $view;

That's much more intuitive for developers.
This commit is contained in:
Bharat Mediratta
2010-07-31 21:16:17 -07:00
parent be4ad8e96d
commit 7607e1f932
25 changed files with 114 additions and 88 deletions

View File

@@ -57,7 +57,7 @@ class Users_Controller extends Controller {
json::reply(array("result" => "success",
"resource" => url::site("users/{$user->id}")));
} else {
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string)$form));
}
}
@@ -92,7 +92,7 @@ class Users_Controller extends Controller {
log::warning("user", t("Failed password change for %name", array("name" => $user->name)));
$name = $user->name;
module::event("user_auth_failed", $name);
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string)$form));
}
}
@@ -126,7 +126,7 @@ class Users_Controller extends Controller {
log::warning("user", t("Failed email change for %name", array("name" => $user->name)));
$name = $user->name;
module::event("user_auth_failed", $name);
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string)$form));
}
}
@@ -136,7 +136,7 @@ class Users_Controller extends Controller {
access::forbidden();
}
json::reply(array("form" => (string) $this->_get_edit_form($user)));
print $this->_get_edit_form($user);
}
public function form_change_password($id) {
@@ -145,7 +145,7 @@ class Users_Controller extends Controller {
access::forbidden();
}
json::reply(array("form" => (string) $this->_get_change_password_form($user)));
print $this->_get_change_password_form($user);
}
public function form_change_email($id) {
@@ -154,7 +154,7 @@ class Users_Controller extends Controller {
access::forbidden();
}
json::reply(array("form" => (string) $this->_get_change_email_form($user)));
print $this->_get_change_email_form($user);
}
private function _get_change_password_form($user) {
@@ -231,7 +231,7 @@ class Users_Controller extends Controller {
$locales = array_merge(array("" => t("« none »")), $locales);
$selected_locale = ($user && $user->locale) ? $user->locale : "";
$form->dropdown("locale")
->label(t("Language Preference"))
->label(t("Language preference"))
->options($locales)
->selected($selected_locale);
}