Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    I would like to encode SortSpecifier[] for persistence via REST

    I would like to encode SortSpecifier[] for persistence via REST Datasource.

    Actually I have form tied to a RestDataSource. I like to persist the user-specified SortSpecifier[] (via MultiSortDialog.askForSort) as a JSON string. I'm getting stuck figuring out how to convert the SortSpecifier[] to a JSON string. Any help would be greatly appreciated!

    #2
    Did you already find JSONEncoder?

    Comment


      #3
      I think this works ... can u confirm I am using these correctly?

      Code:
      MultiSortDialog.askForSort( listGrid(), null, new MultiSortCallback() {
      
      	public void execute( SortSpecifier[] sortLevels )
      	{
      		String debugPrefix = "MultiSortCallback.execute: ";
      
      		ArrayList<JavaScriptObject> array = new ArrayList<JavaScriptObject>();
      
      		for ( SortSpecifier sortLevel : sortLevels )
      		{
      			JavaScriptObject jsObj = sortLevel.getJsObj();
      			array.add( jsObj );
      		}
      
      		JavaScriptObject jsObject = JSOHelper.arrayConvert( array.toArray( new JavaScriptObject[array.size()] ) );
      		JSONEncoder jsonEncoder = new JSONEncoder();
      		String json = jsonEncoder.encode( jsObject );
      		SC.logWarn( debugPrefix + "json=" + json );
      	}
      }  );
      Results in ...

      Code:
      json=[
          {
              "property":"f1", 
              "direction":"ascending"
          }, 
          {
              "property":"f2", 
              "direction":"descending"
          }
      ]
      Now, how to set it to a record attribute to be sent to the REST service for persistence.

      Then, how to reconstitute it to a SortSpecifier[] to apply to a ListGrid?
      Last edited by RobertHana; 30 Jan 2014, 20:24.

      Comment


        #4
        Now that the JSON is a String it can be stored like any other String, eg, Record.setAttribute(someFieldName, jsonString) if you are going to call something like DataSource.addData(Record).

        To deserialize, after calling decode(), call "new SortSpecifier(JavaScriptObject)" with each JavaScript object in the Array.

        Comment


          #5
          Also, we were assuming you had some special purpose in mind.. you do deserialize that get/setViewState() will allow you to save and restore a whole range of ListGrid-related state, including sort specifiers but also field order, field visibility, etc, with less effort than what you're doing now just for sort specifiers?

          Comment

          Working...
          X