In Coffeescript, how to construct a list of object using list comprehension? - coffeescript

I tried to create a list of objects using comprehension in Coffeescript..
photos =
lat: r.latitude
lng: r.longitude
url: r.url
caption: r.contentName
thumbnail: r.url
video: null
for r in res
But it will not work.. Does anyone have ideas about this?

That code creates a single object in photos and then loops over res without the loop doing anything. You want to say:
photos = for r in res
lat: r.latitude
lng: r.longitude
url: r.url
caption: r.contentName
thumbnail: r.url
video: null
If you wanted to do it in the one-liner form, you'd have to say:
photos = (lat: r.latitude, ... for r in res)
with a big mess to create the object in place of the ... of course. And yes, the parentheses are required if you wanted to do it that way due to the high precedence of assignment.

Related

Why L.polygon works in a way but not in another

Good day,
I am sorry if my question could be basic but I think I do have a problem to convert a string to an array.
I am trying to add a polygon to a map (or to a layer). I extracted some polygon coordinates from a MYSQL database to show it into a map.
First here is my code:
function getGeofences(devise_id){
$.ajax({
type: "POST",
url: "maps/sql/getGeofences.php",
data: {devise_id:devise_id},
success: result,
error: error,
dataType: "json"
});
function error(data)
{
console.log("Error getGeofences");
console.log(data);
}
function result(data){
console.log("Geofences from DB");
for (var i = data.features.length - 1; i >= 0; i--) {
console.log(data.features[i].geometry.coordinates);
//var polygon = L.polygon(data.features[i].geometry.coordinates, {color: 'red'});
var polygon = L.polygon([[51.436888577205,-1.373291015625],[51.169011079452,-1.494140625],[51.158676864424,-0.8624267578125],[51.505323411493,-0.889892578125],[51.436888577205,-1.373291015625]] , {color: 'red'});
polygon.addTo(map);
}
}
}
This:
console.log(data.features[i].geometry.coordinates);
shows/prints that:
"[[51.436888577205,-1.373291015625],[51.169011079452,-1.494140625],[51.158676864424,-0.8624267578125],[51.505323411493,-0.889892578125],[51.436888577205,-1.373291015625]]"
I do not understand why this
var polygon = L.polygon([[51.436888577205,-1.373291015625],[51.169011079452,-1.494140625],[51.158676864424,-0.8624267578125],[51.505323411493,-0.889892578125],[51.436888577205,-1.373291015625]] , {color: 'red'});
works!
But this:
var polygon = L.polygon(data.features[i].geometry.coordinates, {color: 'red'});
does not work!
Keeping in mind, this:
data.features[i].geometry.coordinates
print that:
[[51.436888577205,-1.373291015625],[51.169011079452,-1.494140625],[51.158676864424,-0.8624267578125],[51.505323411493,-0.889892578125],[51.436888577205,-1.373291015625]]
I think, it is because the content of
data.features[i].geometry.coordinates
is, in fact, a string with several '[[]]'
Now, I do not know how to convert the above string to an rwally array. So, I do not know what is exactly true in my written, but I need to "parse" the content of
data.features[i].geometry.coordinates
and I do not know how!
Would you suggest me a solution?

Windev - API Rest

I am using the Trello API and I would Like to create a new card on a specific list.
I did it but not the way I wanted
https://api.trello.com/1/lists/idList/cards?key=<myKey>&token=<myToken>&name=My+new+card+name&desc=My+new+card+description
I would like to create a new card in my list nevertheless I want to do it by using this :
newCard = [
{
name: "myname",
idList: "myIdList",
desc: "mydesc",
pos: "top",
due: null
};
]
Because with the first methode I end up with errors when I try to add spaces in the name or description (which is normal, can't have spaces in url)
However, when I try to fulfill the newCard variable it raises an error such as "invalid value for idList" and I don't know why?
Maybe someone can give me an exemple on how to do that?
Have you tried without "[]" ?
On the api doc
https://developers.trello.com/get-started/start-building#create :
var newCard = {
name: 'New Test Card',
desc: 'This is the description of our new card.',
// Place this card at the top of our list
idList: myList,
pos: 'top'
};
And for the list ID it must look similar to "https://trello.com/c/DcqBrqdx/1-target-card.json"

Using typeahead.js to return list of Facebook friends

I am using the Facebook Graph API to return a list of Facebook friends. I then want to run the returned JSON into Typeahead.js as follows:
$(document).ready(function () {
$('input.friends').typeahead({
name: 'friends',
prefetch: 'https://graph.facebook.com/me/friends?access_token=<?php echo $access_token;?>',
ttl: 0,
template: [
'<p class="name-tag">{{name}}</p>'
].join(''),
engine: Hogan
});
});
My corresponding HTML is as follows:
<input class="friends typeahead" type="text" placeholder="Start typing" id="friends">
But nothing is being returned using the prefetch (with hardcoded, local values, no problem). I am not seeing any errors in the console regarding cross-domain issues.
I am fairly sure this is because Typeahead hasn't been told how to handle the JSON structure, but I am unsure how to achieve this. I have tried implementing the templating system Hogan (which I will admit to being unfamiliar with) but this has not helped.
Any ideas much appreciated.
Many thanks
You need to either use valueKey if the result is a simple list of objects, from which you want to use a specific key as your values, or alternatively use a filter: to convert the result into a flat list of suggestions.
In your case, the response is a object with a data member that is a list of name, id pairs. You can have filter() be a function that returns response.data (to extract the list from the data member), and then set valueKey to name.
Thanks Nitzan.
My code snippet currently looks like:
valueKey: 'name',
remote: {
url: 'https://graph.facebook.com/me/friends?access_token=<?php echo $access_token;?>',
filter: function (response) {
return response.data;
},
},
template: [
'<p>{{name}}</p>',
].join(''),
engine: Hogan
Which returns ALL the names in the JSON at once, no matter what is in the input box.
Is this a problem with the filter or something else?
Solution below. Remember to include Hogan if you're using that as your templating engine, as I have done:
$.get('https://graph.facebook.com/me/friends?access_token=<?php echo $access_token;?>', function(server_data){
var rt = {};
for (var i in server_data.data)
rt[server_data.data[i].id] = {name:server_data.data[i].name, id:server_data.data[i].id},
//rt.push({name:server_data.data[i].name})
console.log(rt)
$('input.friends').typeahead({
limit: 10,
name: 'friends',
valueKey: 'name',
local:rt,
template: [
'{{id}}',
].join(''),
engine: Hogan
});
})

Calling Mapquest with an address

I'm looking for a simple way to bring up a MapQuest map with something like:
http://mkapquestapi.com?address=123 Mapel St. Mytown, CA&diameter=20 miles
All the MapQuest documentation I've found tries to put me through hundreds of pages that do much more than I want.
I came across the example below in another StackOverflow question:
$.ajax({
url: "http://www.mapquestapi.com/geocoding/v1/address?key=",
dataType: 'json',
type: 'POST',
contentType:'json',
data: {location: { "postalCode": "98765"}, options: { thumbMaps: false} },
success: function(data) { log( data ) },
error: function(data) { log( 'error occurred - ' + zipCode + ' - ' + data ) }
});
I could handle something like that if someone could point me to where MapQuest explains what it wants in the parameters.
Thanks for any help.
So you want to open MapQuest with an address, rather than lat/long coordinates?
The parameter options are defined in the MapQuest geocoding parameters section. Try this:
http://www.mapquestapi.com/geocoding/#parameters
Disclaimer: I work at MapQuest.
What exactly are you looking to do --- are you looking to generate an image of a map with an address marked on it?
If so, you can use the Static Map API. Here's an example: http://www.mapquestapi.com/staticmap/v4/getplacemap?key=YOUR_KEY_HERE&location=1555 Blake St, Denver, CO&size=400,200&type=map&zoom=13&imagetype=jpeg&showicon=red_1-1
More documentation is here: http://www.mapquestapi.com/staticmap/
If you are looking to make a link that opens MapQuest.com and displays an address, you can link to it this way: http://mapq.st/map?q=1555 Blake St Denver CO 80202&maptype=map
More documentation is here: http://www.mapquestapi.com/link-to-mapquest/
Let me know if this helps. Sounds like you're looking for a map or a map image and not a geocoding result.

PhoneGap iPhone: how to pass back an array of JSON objects using the PluginResult API

I need to pass back an array of JSON object in the similar to the following way,
From iPhone's plugin to the JavaScript side using the Successful callback mechanism on PhoneGap 1.0.0.
I have success on passing back 1 single JSON object but cannot find out how to do any array.
Any idea? Thanks ahead.
[ {name: "abc", number: 1234567890}, {name: "xyz", number: 9876543210} ].
You could try passing it back to the JavaScript as an actual JSON string and parsing it there?
var onSuccess = function(data) {
var incomingData = JSON.parse(data);
// do something with incomingData...
};