diff options
-rw-r--r-- | modules/comment/js/comment.js | 2 | ||||
-rw-r--r-- | modules/tag/controllers/tags.php | 26 | ||||
-rw-r--r-- | modules/tag/js/tag.js | 13 |
3 files changed, 23 insertions, 18 deletions
diff --git a/modules/comment/js/comment.js b/modules/comment/js/comment.js index 7af70047..3ae23f62 100644 --- a/modules/comment/js/comment.js +++ b/modules/comment/js/comment.js @@ -4,7 +4,7 @@ $("document").ready(function() { function ajaxify_comment_form() { $("#gComments form").ajaxForm({ - dataType: 'json', + dataType: "json", success: function(data) { $("#gComments form").replaceWith(data.form); ajaxify_comment_form(); diff --git a/modules/tag/controllers/tags.php b/modules/tag/controllers/tags.php index d87ab8e8..69bc9a48 100644 --- a/modules/tag/controllers/tags.php +++ b/modules/tag/controllers/tags.php @@ -55,19 +55,23 @@ class Tags_Controller extends REST_Controller { } public function _create($tag) { - $form = tag::get_add_form($this->input->post('item_id')); + rest::http_content_type(rest::JSON); + $item = ORM::factory("item", $this->input->post("item_id")); + access::required("edit", $item); + + $form = tag::get_add_form($item->id); if ($form->validate()) { - $item = ORM::factory("item", $this->input->post("item_id")); - if (access::can("edit", $item)) { - tag::add($item, $this->input->post("tag_name")); - rest::http_status(rest::CREATED); - rest::http_location(url::site("tags/{$tag->id}")); - } else { - $form->inputs["add_tag"]->inputs["tag_name"]->add_error("permission denied", 1); - } - } + tag::add($item, $this->input->post("tag_name")); - print $form; + print json_encode( + array("result" => "success", + "resource" => url::site("tags/{$tag->id}"), + "form" => tag::get_add_form($item->id)->__toString())); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); + } } public function _delete($tag) { diff --git a/modules/tag/js/tag.js b/modules/tag/js/tag.js index 74e48998..92f585a5 100644 --- a/modules/tag/js/tag.js +++ b/modules/tag/js/tag.js @@ -3,16 +3,17 @@ $("document").ready(function() { }); function ajaxify_tag_form() { - $("#gAddTagForm").ajaxForm({ - complete: function(xhr, statusText) { - $("#gAddTagForm").replaceWith(xhr.responseText); - if (xhr.status == 201) { + $("#gTag form").ajaxForm({ + dataType: "json", + success: function(data) { + $("#gTag form").replaceWith(data.form); + ajaxify_tag_form(); + if (data.result == "success") { $.get($("#gTagCloud").attr("src"), function(data, textStatus) { $("#gTagCloud").html(data); }); - $("#gAddTagForm").clearForm(); } - ajaxify_tag_form(); + $("#gTag form").clearForm(); } }); } |