oboe.js icon indicating copy to clipboard operation
oboe.js copied to clipboard

Read json object instead of file

Open Akshita-Negi opened this issue 8 years ago • 6 comments

Is there a way to read a json object instead of json file.

Akshita-Negi avatar Dec 02 '16 09:12 Akshita-Negi

@Akshita-Negi what's your goal, what are you trying to do? I ask because oboe is a json parsing library. If you already have a json object, I don't think you need to do any parsing

JuanCaicedo avatar Dec 02 '16 12:12 JuanCaicedo

@JuanCaicedo I can't speak for @Akshita-Negi but I'd find this helpful too, for the purposes of caching.

I'm using Oboe to handle loading data for parts of my user interface. The AJAX call talks to my WordPress backend to get the data I need, then I have several .node() calls to handle adding all the bits of data to the appropriate parts of the UI.

What I'd like to do then, is use .done() to set the entire JSON response from the backend into local storage. Then, on a return visit, I'd use the locally-stored version instead of talking to the backend again. For convenience, it would be better to pass that data into Oboe again instead of writing separate code to add the data to the UI again.

A rough example of how I'd like to accomplish this:

// Retrieve locally-stored version if it exists
var ls = localStorage.getItem( 'item_key' );

// Create an Oboe call using the local data if it exists, or failing over to making the AJAX call
oboe( ls !== null ? ls : ajax_url )
  .node( 'node', function( node ) {
    // Add relevant data to the appropriate bits of the UI
  } )
  // etc
  .done( function( data ) {
    // Store the data in localStorage
    localStorage.setItem( 'item_key', JSON.stringify( data ) );
  } );

Thoughts?

jono-hayward avatar Nov 20 '17 00:11 jono-hayward

@jono-hayward I think what you've written here should already work. Here's a demo, feel free to play around with it https://orchid-death.glitch.me/

JuanCaicedo avatar Nov 21 '17 12:11 JuanCaicedo

any news on this? I'm needing this exact same thing, I need to pass oboe a BIG json string directly to parse into an object.

L3V147H4N avatar Nov 09 '18 14:11 L3V147H4N

I'm not doing any active work on Oboe, but can provide support if you want to take this on 😀 https://github.com/jimhigson/oboe.js/issues/137

JuanCaicedo avatar Nov 09 '18 15:11 JuanCaicedo

nvm, decided to pass on a stream instead thx!

L3V147H4N avatar Nov 13 '18 21:11 L3V147H4N