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

@@ -56,12 +56,12 @@ class Admin_Users_Controller extends Admin_Controller {
message::success(t("Created user %user_name", array("user_name" => $user->name)));
json::reply(array("result" => "success"));
} else {
json::reply(array("result" => "error", "form" => (string) $form));
print json::reply(array("result" => "error", "html" => (string)$form));
}
}
public function add_user_form() {
json::reply(array("form" => (string) $this->_get_user_add_form_admin()));
print $this->_get_user_add_form_admin();
}
public function delete_user($id) {
@@ -81,7 +81,7 @@ class Admin_Users_Controller extends Admin_Controller {
$name = $user->name;
$user->delete();
} else {
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string)$form));
}
$message = t("Deleted user %user_name", array("user_name" => $name));
@@ -95,7 +95,7 @@ class Admin_Users_Controller extends Admin_Controller {
if (empty($user)) {
throw new Kohana_404_Exception();
}
json::reply(array("form" => (string) $this->_get_user_delete_form_admin($user)));
print $this->_get_user_delete_form_admin($user);
}
public function edit_user($id) {
@@ -136,7 +136,7 @@ class Admin_Users_Controller extends Admin_Controller {
message::success(t("Changed user %user_name", array("user_name" => $user->name)));
json::reply(array("result" => "success"));
} else {
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string) $form));
}
}
@@ -146,7 +146,7 @@ class Admin_Users_Controller extends Admin_Controller {
throw new Kohana_404_Exception();
}
json::reply(array("form" => (string) $this->_get_user_edit_form_admin($user)));
print $this->_get_user_edit_form_admin($user);
}
public function add_user_to_group($user_id, $group_id) {
@@ -194,12 +194,12 @@ class Admin_Users_Controller extends Admin_Controller {
t("Created group %group_name", array("group_name" => $group->name)));
json::reply(array("result" => "success"));
} else {
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string)$form));
}
}
public function add_group_form() {
json::reply(array("form" => (string) $this->_get_group_add_form_admin()));
print $this->_get_group_add_form_admin();
}
public function delete_group($id) {
@@ -215,7 +215,7 @@ class Admin_Users_Controller extends Admin_Controller {
$name = $group->name;
$group->delete();
} else {
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string) $form));
}
$message = t("Deleted group %group_name", array("group_name" => $name));
@@ -230,7 +230,7 @@ class Admin_Users_Controller extends Admin_Controller {
throw new Kohana_404_Exception();
}
json::reply(array("form" => (string) $this->_get_group_delete_form_admin($group)));
print $this->_get_group_delete_form_admin($group);
}
public function edit_group($id) {
@@ -263,7 +263,7 @@ class Admin_Users_Controller extends Admin_Controller {
$group->reload();
message::error(
t("Failed to change group %group_name", array("group_name" => $group->name)));
json::reply(array("result" => "error", "form" => (string) $form));
json::reply(array("result" => "error", "html" => (string) $form));
}
}
@@ -273,7 +273,7 @@ class Admin_Users_Controller extends Admin_Controller {
throw new Kohana_404_Exception();
}
json::reply(array("form" => (string) $this->_get_group_edit_form_admin($group)));
print $this->_get_group_edit_form_admin($group);
}
/* User Form Definitions */
@@ -309,7 +309,7 @@ class Admin_Users_Controller extends Admin_Controller {
}
module::event("user_edit_form_admin", $user, $form);
$group->submit("")->value(t("Modify User"));
$group->submit("")->value(t("Modify user"));
return $form;
}
@@ -354,7 +354,7 @@ class Admin_Users_Controller extends Admin_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);
}