Dust.js - iterate through an iterated anonymous array - dust.js

I have this data structure in dust:
{ 'items' : [
[ 'apples', "$1.00" , "Delicious" ],
[ 'oranges', "$2.00" , "Juicy" ],
]
}
I'm trying to access the inner items, and can't figure out how.
I can address the entire array of the current loop via {.} , but i can't seem to access the items within it. ( I could do this in Mustache )
I was expecting something like this to work...
{#items}
<b>{.[0]}</b> <em>only {.[1]}!</em>
<p>{.[2]}</p>
{/items}

When I ran your example in the dust playground: http://linkedin.github.io/dustjs/test/test.html
the output I got was:
<b>apples</b> <em>only $1.00!</em><p>Delicious</p><b>oranges</b> <em>only $2.00!</em><p>Juicy</p>
This looks to be what you were expecting. I believe fixes in this area after your post likely made this work. Grab the latest version of dust.

Related

Mongoid Return Specific Object from Array in Document

This seems as though it should be simple but I have been struggling with this for a while with no luck.
Let's assume I have a simple document that looks like the following:
{
data: [
{
name: "Minnesota",
},
{
name: "Mississippi",
},
...
]
}
If I run the following query in my Mongo Shell, everything works as I would expect:
db.collection.find({}, {data: {$elemMatch: {name: "Michigan"}}})
Returns:
{ "_id" : ObjectId("5e9ba60998d1ff88be83fffe"), "data" : [ { "name" : "Michigan" } ] }
However, using mongoid, attempting to run a similar query returns every object inside of the data array. Here is one of the may queries I've tried:
Model.where({data: {"$elemMatch": {name: "Michigan"}}}).first
As I mentioned above, that little query returns everything inside the data array, not the specific object I'm trying to pull out of the document.
Any help would be appreciated. I'm trying to avoid returning the results and post-processing them with Ruby. I'd love to handle this at the DB level.
Thank you.
There was a very similar question earlier for a different driver. Apparently the ruby driver behaves differently than the shell.
Try running your find as the equivalent database command:
session.command({'find' => 'my_collection', 'filter' => {}, projection => {data: {$elemMatch: {name: "Michigan"}}}})
Mongoid syntax for projections is only.

Dart Angular Components - How to get full path of selected values?

I'm implementing a Single Selection Nested Tree from the Angular Components Example and the example code gets the selected value.
I want to get the full path with the selected value as well as the selected parent groups titles.
Is this possible? I've been searching for an API documentation but haven't found one yet.
Example:
<material-tree
[options]="nestedOptions"
[expandAll]="expandAll"
[selection]="singleSelection">
</material-tree>
-
final SelectionOptions nestedOptions = new _NestedSelectionOptions([
new OptionGroup(
['Animated Feature Films', 'Live-Action Films', 'Documentary Films'])
], {
'Animated Feature Films': [
new OptionGroup([
'Cinderalla',
'Alice In Wonderland',
'Peter Pan',
'Lady and the Tramp',
])
],
'Live-Action Films': [
new OptionGroup(
['Treasure Island', 'The Littlest Outlaw', 'Old Yeller', 'Star Wars'])
],
'Documentary Films': [
new OptionGroup(['Frank and Ollie', 'Sacred Planet'])
],
'Star Wars': [
new OptionGroup(['By George Lucas'])
],
'By George Lucas': [
new OptionGroup(
['A New Hope', 'Empire Strikes Back', 'Return of the Jedi'])
]
});
There isn't a built in way to display the path information. To make that work you would want to create a class for your values that stores the parent information and makes that accessible. Then use an ItemRenderer to ensure it is displayed as a string when used in the Material Tree.

How to iterate over this Json data in rails?

I'm trying to Iterate over this Json data I want to get the Items out of "data I tried doing
#Response =HTTParty.get(("http://ddragon.leagueoflegends.com/cdn/5.15.1/data/en_US/item.json"))
puts #Response["data"].each do |item|
puts item
end
Also I want to be able to iterate them without having to know the IDS prior for example the first item is "1001" I don't want to have to enter those manually
but this just says that it can't find anything with '[]' for Nil:NilClass
Also before anyone mentions the JSON below doesn't finish because I only took a sample there are a lot more Items and if I were to paste it , it would easily be over 1000 Lines.
"type":"item",
"version":"5.15.1",
"basic":{},
"data":{
"1001":{
"name":"Boots of Speed",
"group":"BootsNormal",
"description":"<groupLimit>Limited to 1.</groupLimit><br><br> <unique>UNIQUE Passive - Enhanced Movement:</unique> +25 Movement Speed<br><br> <i>(Unique Passives with the same name don't stack.)</i>",
"colloq":";",
"plaintext":"Slightly increases Movement Speed",
"into":[
"3006",
"3047",
"3020",
"3158",
"3111",
"3117",
"3009"
],
"image":{
"full":"1001.png",
"sprite":"item0.png",
"group":"item",
"x":0,
"y":0,
"w":48,
"h":48
},
"gold":{
"base":325,
"purchasable":true,
"total":325,
"sell":227
},
"tags":[
"Boots"
],
"stats":{
"FlatMovementSpeedMod":25.0
}
},
"1004":{
"name":"Faerie Charm",
"description":"<stats><mana>+25% Base Mana Regen </mana></stats>",
"colloq":";",
"plaintext":"Slightly increases Mana Regen",
"into":[
"3028",
"3070",
"3073",
"3114"
],
"image":{
"full":"1004.png",
"sprite":"item0.png",
"group":"item",
"x":48,
"y":0,
"w":48,
"h":48
},
"gold":{
"base":180,
"purchasable":true,
"total":180,
"sell":126
},
"tags":[
"ManaRegen"
],
"stats":{
}
},
Four problems:
you are not parsing the response body with JSON.parse
you are using a constant name (starting with capital letter) as a variable name (#Response), you shouldn't.
unnecessary puts on iteration.
The value of data is not an array, but rather another hash. So you should iterate over it as key/value pairs.
Solution:
#response = JSON.parse(HTTParty.get(your_url).body)
#response["data"].each do |key, value|
puts key
puts value
end
I had done something like this before, maybe that can help you here is the link - Reading Json using Ruby

Dart List within a Map

I have a Map in Dart (originally loaded from JSON) that looks something like this:
somevar = {
'Title': 'Some object',
'items': [{'title': 'Item 1 Title'}, {'title': 'Item 2 Title'}]
}
For some reason somevar['items'] doesn't behave quite like a list.
I get Exception: NoSuchMethodError : method not found: 'iterator' if I attempt to iterate over the list.
I also get a similar error if I try somevar['items'].length
If I manually load this "list" like this: someList = new List(somevar['items']); then it works as expected.
Any idea why this is that case, and what I'm doing wrong? For me the natural expectation would be that a "list" parsed from JSON will behave exactly like the List() object.
Never mind, seems that I had a deeper issue in my code that cause my somevar variable to be null (even though it should have the map.
Anyway, I'm marking this as solved for now so not to waste anyone's time.

Storing graph-like structure in Couch DB or do include_docs yourself

I am trying to store network layout in Couch DB, but my solution provides rather randomized graph.
I store a nodes with a document:
{_id ,
nodeName,
group}
and storing links in traditional:
{_id, source_id, target_id, value}
Following multiple tutorials on handling joins and multiple relationship in Couch DB I created view:
function(doc) {
if(doc.type == 'connection') {
if (doc.source_id)
emit("source", {'_id': doc.source_id});
if(doc.target_id)
emit("target", {'_id': doc.target_id});
}
}
which should have emitted sequence of source and target id, then I pass it to the list function with include_docs=true, assumes that source and target come in pairs stitches everything back in a structure like this:
{
"nodes":[
{"nodeName":"Name 1","group":"1"},
{"nodeName":"Name 2","group":"1"},
],
"links": [
{"source":7,"target":0,"value":1},
{"source":7,"target":5,"value":1}
]
}
Although my list produce a proper JSON, view map returns number of rows of source docs and then target docs.
So far I don't have any ideas how to make this thing working properly - I am happy to fetch additional values from document _id in the list, but so far I havn't find any good examples.
Alternative ways of achieving the same goal are welcome. _id values are standard for CouchDB so far.
Update: while writing a question I came up with different view which sorted my immediate problem, but I still would like to see other options.
updated map:
function(doc) {
if(doc.type == 'connection') {
if (doc.source_id)
emit([doc._id,0,"source"], {'_id': doc.source_id});
if(doc.target_id)
emit([doc._id,1,"target"], {'_id': doc.target_id});
}
}
Your updated map function makes more sense. However, you don't need 0 and 1 in your key since you have already "source"and "target".

Resources