Find all parents elements in a Json file using jQuery - javascript

I'm currently working on a recursive menu which is built on top of jQuery which looks quite good already.
The structure of the JSon file containing the menu looks as the following:
[
{
"Id": "menuOfficeWebControlsForWebApplication",
"Title": "Office Web Controls",
"Resource": "/Documentation/Data/index.html" },
{
"Id": "menuGettingStarted",
"Title": "Getting Started",
"Resource": "/Documentation/Data/getting-started.html",
"Categories": [{
"Id": "menuCompilingFromSource",
"Title": "Compiling From Source",
"Resource": "/Documentation/Data/Getting-Started/compiling-from-source.html"
},{
"Id": "menuDownloadReleasePackage",
"Title": "Download Release Package",
"Resource": "/Documentation/Data/Getting-Started/downloading-release-package.html"
},{
"Id": "menuBuildingYourFirstApplication",
"Title": "Building your first application",
"Resource": "/Documentation/Data/Getting-Started/building-your-first-application.html"
}]
}
]
Now, I can retrieve an item out of the menu using jQuery and the result might be this item:
{
"Id": "menuBuildingYourFirstApplication",
"Title": "Building your first application",
"Resource": "/Documentation/Data/Getting-Started/building-your-first-application.html"
}
Now, I want to retrieve all the elements which are at a higher level and all the items which are directly below that item.
Any help is highly appreciated.

JQuery is for querying HTML elements from within the DOM of the current document, not for traversing objects or JSON expression strings.
In any case, given an object, there is no way to "discover" any objects, variables, or arrays that might hold a reference to it.
I'd recommend picking up a decent Javascript book and becoming familiar with the basics. Maybe stay away from things like JQuery at first as they can confuse things for you.

Related

Socrata map results in $http request, Javascript

I am trying to use this dataset to determine which district boundary an address (passed into the API call) falls within.
The endpoint returns an array of objects for each district or council. The polygon is found within the "the_geom" property, with 2 properties - type and coordinates. I have tried using $where, but I get errors.
[
{
"comments": "Inaugurated 2015-06-22",
"council": "1",
"councilper": "Scott Griggs",
"district": "1",
"objectid": "1",
"shape_area": "343352603.892",
"shape_leng": "88541.3042539",
"the_geom": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-96.80995700065864,
32.77138899977414
],
[
-96.80969800043205,
32.77121999997131
],
[ ...
I tried to use the query below, but it gave me an error:
https://www.dallasopendata.com/resource/h9ws-fqcn.json?$where=within_polygon(the_geom, 'MULTIPOLYGON (((-96.800270, 32.779091)))')
This is the page reference page - https://www.dallasopendata.com/Geography-Boundaries/Adopted-Council-Districts/6dcw-hhpj
And this is the endpoint- https://www.dallasopendata.com/resource/dgxr-hmze.json
any help would be greatly appreciated.
I suspect you're the developer who popped into our IRC channel, but I'll answer here too!
You're pretty close here! What you want to do here is use the intersects(...) SoQL function with a Well Known Text (WKT) POINT.
Here's an example that works for your use case:
https://www.dallasopendata.com/resource/h9ws-fqcn.json?$where=intersects(the_geom,%20%27POINT%20(-96.7994007%2032.775765)%27)

Structure blog posts by using post ids as id attribute

How do I structure blog posts/comments in HTML so that I can select them later on using jQuery/Javascript for updating/deleting/Ajax calls?
My instinct is to use those posts'/comments' IDs (primary key in the database) as their id= attributes in HTML e.g.
<section class="posts">
<div id=**post's ID (primary key) here**> post </div>
</section>
Is this a bad thing? (exposing details to the web such as primary keys, etc) or not necessarily?
I don't see a problem with that. Assuming your server returns a list of entities in JSON:
{
"comments": [
{ "id": "001", "author": "Jack", "comment": "Hello" },
{ "id": "002", "author": "Jill", "comment": "there" }
],
"posts": [
{ "id": "003", "author": "Greg", "comment": "This is an awesome website." },
]
}
You could use the id property of each to store it in the HTML for later retrieval:
<div id='001'>Hello</div>
<div id='002'>there</div>
Remember HTML ids are global across the entire page, so your ids across comments, posts, and any other entities present on the page must be unique. It might be a good idea to prefix them to avoid collisions, like comment001, comment002, post001, etc.
This is essentially what other frameworks do (like React) behind the scenes.

Using javascript, is it possible to pull and list all available languages through Google Translate?

I'm tinkering with a site that has a sort of module/extension that lets you turn a translator bar on/off. It appears on every page like a persistent notification bar, and lists languages you can pick. Right now, the translator.js file being used has an array that lists available languages by using the Google Language codes: https://developers.google.com/translate/v2/using_rest#language-params
Instead of manually coding in the language codes to be used, is there a way to just get them all from Google and then put them into an array?
GET https://www.googleapis.com/language/translate/v2/languages?key=YOUR_KEY&target=LANGUAGE_FOR_NAMES
{
"data": {
"languages": [
{
"language": "af",
"name": "Afrikaans"
},
{
"language": "sq",
"name": "Albanian"
},
...
{
"language": "yo",
"name": "Yoruba"
},
{
"language": "zu",
"name": "Zulu"
}
]
}
}

Retrieve the value from a JSONP formatted data object

Just when I think I've got he hang of identifying an element in an object, I run into a scenario that I cannot seem to get the value I want.
This part works and the data returned is correct: I have a map and when I attempt to identify a building on the map, I receive the following json object (this has been shortened for readability but in real life, its properly formatted): The function MapClick(queryResults) is called when the map is clicked.
dojo.io.script.jsonp_dojoIoScript19._jsonpCallback({
"results": [
{
"layerId": 5,
"layerName": "Building",
"value": "Name of item clicked",
"displayFieldName": "name",
"attributes": {
"ID": "123",
"name": "Name of item clicked",
"Variable1": "Some bit of information",
"Variable2": "Some other bit of information",
...
...
All I'm trying to do is return either the results[0].value OR results[0].attributes.name which in this example should bring back "Name of item clicked". The layerId, layerName, value, and displayFieldName are the "common most accessed data" so they are returned but the same information is also found within the attributes.
I've tried console.log(results[1].attributes.name); and console.log(results) with no success.
Turns out the name of the function handling the MapClicked is queryResults was needed so the correct answer is: queryResults[0].value and when you see open brackets [, you can be sure you will need [ some number ] (e.g. queryResults[0].value or queryResults[99].someothervariable. Or at least I think this is a correct statement.

Facebook Graph API - get page feed + full event info?

Is it possible to, in 1 request, get the feed of a page but with the full event info?
As it is now, if a shared event is posted, you only get back the link to that event, no picture or title:
{
"id": "xxx",
"from": {
"category": "Community",
"name": "xxx",
"id": "xxx"
},
"story": "xxx shared xxx's event.",
"link": "https://www.facebook.com/events/xxx/",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/xxx/posts/xxx"
},
{
"name": "Like",
"link": "https://www.facebook.com/xxx/posts/xxx"
}
],
"privacy": {
"value": ""
},
"type": "link",
"status_type": "shared_story",
"application": {
"name": "Links",
"id": "xxx"
},
"created_time": "2013-06-19T10:05:50+0000",
"updated_time": "2013-06-19T10:05:50+0000",
"likes": {
"data": [
{
"name": "xxx",
"id": "xxx"
}
],
"count": 1
}
}
If I understand correctly, you need to retrieve the events but you want to do it all at once with the feed because you want to retrieve the information on the feed anyway.
Before doing that, you must know that the feed doesn't contain all the events.... Once created, a link to the event is automatically shared on the page feed. It is only a reference, which can then be hidden. The event won't be displayed on the feed anymore even if it still exists.
Requesting two different objects at the same time
So, the feed doesn't have the events information and the events and posts (feed) are stored on 2 different tables. Therefore, you need to get the events independently from the feed:
The feed /PAGE_ID/feed
The events /PAGE_ID/events
And, as you wanted, Graph API allows you to do this in only one request:
/PAGE_ID?fields=feed,events
Additional fields
Note that either feed or events accept the limit and fields parameters. For example, events can be specified by:
events.limit(100).fields(location,name,owner,description,updated_time,venue)
Possible fields are given in the doc.
There is no way to get the "full info" at once. You will have to specify each field in the request. So, don't get the "full info", but just the information you really need.
There is a post from Facebook addressing this scenario using multi-queries and FQL (Facebook Query Language). This will allow you to make multiple FQL calls in one request.
https://developers.facebook.com/docs/technical-guides/fql/#multi

Categories

Resources