Multiline unbuffered code in jade templates - node.js

I am trying to write a template that renders a double indexed array. So I started writing this:
- var grid = [[1, 0, 1], [0, 1, 0]];
each row in grid
each cell in row
if cell
span x
else
span o
but this is not how I want to write my array
I want to write it like this:
- var grid = [[1, 0, 1],
[0, 1, 0]];
This doesn't work because jade is already out of the inline javascript
- var grid = [[1, 0, 1],
- [0, 1, 0]];
This doesn't work because jade considers those two incorrect lines instead of one line
How can I make it work?

Update: Multiline defs are now working for me using Jade 1.11.0. Even nested JSON now works like a charm.
-
projects = [{
title: "Project 1",
classname: "project1",
slides: [{
title: "Slide 1"
img: "images/hello.png"
},{
title: "Slide 2"
img: "images/world.png"
}]
}, {
title: "Project 2",
classname: "project2",
slides: [{
title: "Slide 3"
img: "images/fun.png"
},{
title: "Slide 4"
img: "images/things.png"
}]
}]

EDIT: Yay, these are real! Go check out the other answer on how to pull this off.
Sadly, this is currently not possible in Jade. TJ (the maintainer) has stated that he does not care about this, but would welcome a feature request. https://github.com/visionmedia/jade/issues/796
Fortunately, you can declare the array in your JS file and pass it as a variable to Jade.

Related

MongoDB query fast in mongo shell but slow/times out in nodejs

The following query takes approximately 54ms in the mongo shell, but times out after a minute in the nodejs driver:
db.posts.find(
{
parentId: 10,
modifiedGmt: {
"$gte": new Date("2017-01-01T00:00:00.000z")
},
postType: {
"$in": ["type1", "type2"]
}
},
{
_id: 1,
parentId: 1,
postId: 1,
url: 1,
modifiedGmt: 1,
authors: 1,
taxonomy: 1,
postType: 1
}
).sort({modifiedGmt: 1}).limit(2400)
Explain shows that the query is using existing indexes. If I drop the limit to something very low like 10, it won't time out but it will take far, far too long. I'm not really sure where to go with this. It's a large collection but with the indexes in place and the limit sub-10000 I don't see why it would be so slow.
Any ideas?
I'm suspecting that the .sort is not able to use your index.
I would recommend to have a read on this page sort-operator-and-performance.

Unable to tap into object fields of Json post data in Nodejs

I am reading the sent, post data to my Nodejs using :
console.log(req.body.order)
It prints the following data in my console:
{res_key: kshdkjfh9827349sjdhf, auth_Key: ksjdhfkjs928374sdjkhf923847, customer_id: f, payment_mode: f, getMenuURL: dataurl, orderUID: dfjkh987, has_paid: 0, delivery_discount: 0, menuOrderData: [{menu_id: 244, menu_price: 4.5, item_name: Cacik (v), menu_quantity: 1, topingsOrder: []}, {menu_id: 240, menu_price: 5, item_name: Marinated Olives (v), menu_quantity: 1, topingsOrder: [{id: 708, name: Water, optional_fee: 2.00}, {id: 710, name: Coke Zero, optional_fee: 2.00}]}, {menu_id: 241, menu_price: 8.5, item_name: Mixed Pickles (v), menu_quantity: 1, topingsOrder: []}]}
My aim is to calculate the total for all items in this data so I would like to tap into it for example, I would like to tap into res_key using:
const data = req.body.order
const res = data['res_key']
Currently I am unable to read any parameter. Need help resolving this please.
My nodejs app settings are:
app.use(express.urlencoded({extended:true})); app.use(express.json());

Referencing an object array for cascading select options with angular-formly

I'm creating a form which will have field options dependent upon choices earlier in the form, referencing a JSON array. I've studied the Cascade Select example, but haven't quite wrapped my head around how the controllers work in it. Would someone mind helping me adapt the concepts of the Cascade Select example to reference a JSON array?
Here is a link to a JS Bin illustrating what I'm trying to accomplish (be sure to "Run with JS"). I would like fields whose options are populated with respect to an array and are filtered based on previous selection. I've figured out how to do the first level of options for selecting a sport with a simple for-loop function passed to the "options" argument in the form element; but I need a hand moving to the next stage. Ideally, I would like to have more depth; i.e. Pick a Sport > Pick a Team > Pick a Player; but I should be able to figure it out with just the first filtered select options.
Thank you!
Actually this is totally unrelated to angular-formly. Your data model is not properly modeled, you should use the concept of foreign keys. A more appropriate modeling would be:
var sports = [{
id: 1,
name: 'Soccer'
}, {
id: 2,
name: 'Basketball'
}];
var teams = [{
id: 1,
fk: 1,
name: 'Bayern Munich'
}, {
id: 2,
fk: 1,
name: 'Real Madrid'
}, {
id: 3,
fk: 2,
name: 'Cleveland'
}];
var player = [{
id: 1,
fk: 1,
name: 'Mario Götze'
}, {
id: 1,
fk: 2,
name: 'Javier Hernandez'
}, {
id: 2,
fk: 3,
name: 'LeBron James'
}];
A working example is shown here: http://output.jsbin.com/jinaca

Nested arrays in Mongoose

In the collection I'm working on, a document looks like this:
{
name: 'Myname',
other: 'other',
stuff: [
['something', 12, 4, 'somethingelse'],
['morestuff', 2, 4, 8],
['finally', 12, 'again', 58],
]
}
I wrote this Mongoose schema to access it:
var MyDocSchema = new Schema({
name: String,
other: String,
stuff: [],
});
When I query a doc, everything works well, the output shown in the console is right. But when, I try to do console.log(myDoc.stuff), I got the following:
['something', 12, 4, 'somethingelse', 'morestuff', 2, 4, 8, 'finally', 12, 'again', 58]
instead of
[
['something', 12, 4, 'somethingelse'],
['morestuff', 2, 4, 8],
['finally', 12, 'again', 58],
]
What am I doing wrong? Thank you for your help!!
Disclaimer: This response is pretty dated, 2012! It might not be the most accurate.
From the Mongoose documentation.
http://mongoosejs.com/docs/schematypes.html: Scroll down to the Array section:
Note: specifying an empty array is equivalent to [Mixed]. The
following all create arrays of Mixed.
Details on what that means is in the Mixed section right above the Array section.
Here's what you need to do.
Define a schema for the embedded documents:
var Stuff = new Schema({
name: String,
value1: Number,
...
});
Use that instead of an empty array []:
var MyDocSchema = new Schema({
name: String,
other: String,
stuff: [Stuff],
});

How do i display each word of string with different color in YUI DataTable?

How do i display each word of string with different color in YUI DataTable?
I need "Mark" color is blue and "good" is red.
http://i.stack.imgur.com/OSlvc.png
<script>
YUI().use("datatable", function(Y) {
var records = [
{ item: "widget", cost: 23.57, price: 47.5 },
{ item: "gadget", cost: 0.11, price: "Mark is good " },
{ item: "sprocket", cost: 4.08, price: 3.75 }
];
var table = new Y.DataTable({
columns: ["item","cost", "price"],
data: records
});
table.render("#example");
});
</script>
Use a column formatter. Split the value string by whitespace o.value.split(/\s/) and inspect each word. If it's one of your target words, wrap it with an element. Assign that element a css class appropriate to add the color.
E.g. https://gist.github.com/2872732

Resources