Level: Beginning
Prerequisite: JSAPI User Authentication
Example: Network Stream Example
Advanced Topics: Liking Updates
Summary:This tutorial covers the basic JSAPI network stream call, options for configuration, how to work with the returned object and present it in the browser.
Note that all code samples in this document use jQuery, so that's the syntax being used.
The framework is loaded with the instruction to let the user stay logged in:
And the authentication handler goes on the login button:
A <div> should be created where the output can be injected when it returns.
A very basic network stream call works as follows.
The IN.API.NetworkStream() method can only be called for the current user. IN.API.NetworkStream("me") is the same as IN.API.NetworkStream().
Additional behaviors are chained on as additional methods using the "." format. So in the example above, the method is called with a ".result" function to tell the framework what to do when the result is returned. The .result method can be used with an anonymous function as it is above, or can be given a reference to a different function to call when the results are returned.
Methods which can be chained on general JSAPI methods:
All parameters can be found on the Get Network Updates page. The "type" parameter is particularly useful in the case of network updates. You can use "type" to restrict the type or types of network updates you want to see (such as Shares, Comments, Status Updates). These types are also discussed on the Get Network Updates page.
In the example case, we are restricting the query to SHAR updates (Shares), and we've restricted the number of items returned to 1 (to make this tutorial reasonable in size)
If you check the output for the calls above, you'll see what the returned objects look like. All we have done up until now is print the string version of the object to the browser, but processing those objects is relatively simple.
We'll use as an example the network update call with the SHAR type.
Profile Call
The results are returned as JavaScript objects, which can be operated on using standard JavaScript notation. Whether a single ID or multiple IDs are requested, the results are returned as a list, so you need to access the value you need by looping through the results or directly referencing the index: profile = result.values[0];
You can access each of the fields you requested (or the default fields) to build the HTML you want to inject into your page, and then inject it like any other AJAX content.