Skip to content
This repository
Browse code

MINOR GridField JS API for updating state

  • Loading branch information...
commit 1b42b2afa784bce3c3ef79c46972559ce754fd34 1 parent 47ac047
Ingo Schommer authored February 08, 2012
5  forms/gridfield/GridField.php
@@ -713,6 +713,11 @@ public function _nameEncode($match) {
713 713
 	 * @return string HTML tag
714 714
 	 */
715 715
 	public function Field() {
  716
+		Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/jquery/jquery.js');
  717
+		Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/json-js/json2.js');
  718
+		Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
  719
+		Requirements::javascript('sapphire/javascript/GridField.js');
  720
+
716 721
 		// Store state in session, and pass ID to client side
717 722
 		$state = array(
718 723
 			'grid' => $this->getNameFromParent(),
3  forms/gridfield/GridFieldFilter.php
@@ -54,9 +54,6 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) {
54 54
 	}
55 55
 
56 56
 	public function getHTMLFragments($gridField) {
57  
-		Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
58  
-		Requirements::javascript('sapphire/javascript/GridField.js');
59  
-
60 57
 		$forTemplate = new ArrayData(array());
61 58
 		$forTemplate->Fields = new ArrayList;
62 59
 
3  forms/gridfield/GridFieldPaginator.php
@@ -83,9 +83,6 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) {
83 83
 	 * @return array
84 84
 	 */
85 85
 	public function getHTMLFragments($gridField) {
86  
-		Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
87  
-		Requirements::javascript(SAPPHIRE_DIR.'/javascript/GridField.js');
88  
-		
89 86
 		$forTemplate = new ArrayData(array());
90 87
 		$forTemplate->Fields = new ArrayList;
91 88
 		
3  forms/gridfield/GridFieldSortableHeader.php
@@ -13,9 +13,6 @@ class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataM
13 13
 	 * Returns the header row providing titles with sort buttons 
14 14
 	 */
15 15
 	public function getHTMLFragments($gridField) {
16  
-		Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
17  
-		Requirements::javascript(SAPPHIRE_DIR.'/javascript/GridField.js');
18  
-
19 16
 		$forTemplate = new ArrayData(array());
20 17
 		$forTemplate->Fields = new ArrayList;
21 18
 
15  javascript/GridField.js
@@ -3,6 +3,21 @@ jQuery(function($){
3 3
 	$('fieldset.ss-gridfield').entwine({
4 4
 		getItems: function() {
5 5
 			return this.find('.ss-gridfield-item');
  6
+		},
  7
+		/**
  8
+		 * @param {String}
  9
+		 * @param {Mixed}
  10
+		 */
  11
+		setState: function(k, v) {
  12
+			var state = this.getState();
  13
+			state[k] = v;
  14
+			this.find(':input[name="' + this.data('name') + '[GridState]"]').val(JSON.stringify(state));
  15
+		},
  16
+		/**
  17
+		 * @return {Object}
  18
+		 */
  19
+		getState: function() {
  20
+			return JSON.parse(this.find(':input[name="' + this.data('name') + '[GridState]"]').val());
6 21
 		}
7 22
 	});
8 23
 

0 notes on commit 1b42b2a

Sam Minnée

Something we may need to consider here is whether state should be immediately pushed through to the server, at least if the state is stored in-session. A particular use-case would be pressing Command-R after changing pages.

Sam Minnée

It would be worth adding support for executing a GridField action via JavaScript. We'd need to consider what should be done with the response - it may not always be appropriate to refresh the GridField in its entirety. New values of X-Get-Fragment might be useful here, e.g. 'X-Get-Fragment: Status-JSON-Pair' might just return things like:

{ "success" : 1 }
{ "success' : 0, "message" : "You can't delete that record" }
Sam Minnée

To support this, we could provide an update-state GridField action; my suggestion on line 21 could provide the scaffold for it.

Sam Minnée

I'd say that this is a little too heavily coupled; i'd probably pass through a data-statefieldname attribute. That way we don't need to keep the naming convention of the GridState field name in sync between the back-end and front-end.

I'd also make it explicit at the top of the JS file which custom data attributes this code relies on.

Please sign in to comment.
Something went wrong with that request. Please try again.