0

I have been working with Famo.us just for a short while but now I am in need of consuming some JSON. In jQuery I would use the getJSON method to make the JSON call and get the data back in an object. Is there a way to do this in pure Famo.us? I ask because I have only found examples of jQuery being added to the app to make that JSON call. I am not sure that this is the best practice so I figured maybe someone could point me in the right direction.

$.getJSON('data/data.json', function(json) {
  $.each(json, function(key,data){
    seriesArr.push({ 
      name: data.name, 
      y: data.Count,
      drilldown: data.name
    });
  });
});
Tim Daley
  • 145
  • 3
  • 10

2 Answers2

1

There is a Utility function in famo.us for loading an URL: Utility.loadURL (https://famo.us/docs/utilities/Utility)

var Utility = require('famous/utilities/Utility');

Utility.loadURL('http://example.com', function (content) {

    // Check response
    if (!content) {
        return;
    }

    // Consume response
    var parsedContent = JSON.parse(content);
    ...

});
IjzerenHein
  • 2,690
  • 1
  • 17
  • 13
0

You can certainly use jQuery for making requests in Famo.us. Famo.us is designed as the presentation layer of the application. It does not care how the data gets in or out.

Just some things to keep in mind. When making requests, Try to time them such that all animation is complete. A request no matter the library will cause stuttering.

For instance using the setTransform callback method of a StateModifier..

state.setTransform(transform, transition, function(){
    // Make request
});

So to sum things up, You are on the right path. With vanilla Famo.us you are free to make requests with whichever other library you wish. Just do so in a timely manner!

Good Luck!

johntraver
  • 3,612
  • 18
  • 17
  • 1
    To avoid blocking from XHR in the main event loop, you can perform the XHR requests in a web worker and then use just send the data to the main UI thread. – Andrew De Andrade Jun 18 '14 at 22:08
  • If the messages being sent from the worker to the main UI thread are very large, you may want to investigate transferrable objects as well. They are more efficient that plain vanilla web worker messages. – Andrew De Andrade Jun 18 '14 at 23:29
  • Yup just pocketed an article and saw those as well! Oh so much to learn! – johntraver Jun 19 '14 at 01:45
  • XHR doesn't block. Just FYI. Only the request emission and response parsing blocks. The event pickup from the worker to the main thread is actually equally expensive. Unless you are doing a bunch of data munging to take the JSON from the API and parse it into another form (which can be a bit expensive), you should do API calls from the main thread as there is no real benefit. Just FYI @AndrewDeAndrade – Andrew Rhyne Dec 19 '14 at 22:42