underscore.js

How to evaluate javascript functions in underscore with mustache notation?

爱⌒轻易说出口 提交于 2019-12-22 17:59:10
问题 I want to do something like this: <script id="tmpl-books" type="text/template"> <ul> <% for (var i = 0; i < books.length; i++) { %> <% var book = books[i]; %> <li> <em><%= book.title %></em> by <%= book.author %> </li> <% } %> </ul> </script> in my code, but I am using JSP so I have to use the {{ }} notation, but when I do this {{ for(var... }} does not work. <script id="tmpl-books" type="text/template"> <ul> {{ for (var i = 0; i < books.length; i++) { }} <% var book = books[i]; %> <li> <em>{

Best way to take out keys with invalid (NaN, blank, etc) values from an object?

◇◆丶佛笑我妖孽 提交于 2019-12-22 12:34:07
问题 I've got a short search form that a user fills out. There will be multiple search queries that will go into MongoDB: The form creates a variable called searchParams that may look like this: var searchParams = { city: "Springfield", bedrooms: 3, bathrooms: 2 }; I then have a function that takes the searchParams as an argument and queries Mongo using it: var searchListings = function(searchParams){ return db.MyListings.find(searchParams).fetch(); } db.MyListings.find( {city: "Springfield",

How to display a JS object with underscore templates?

▼魔方 西西 提交于 2019-12-22 10:28:45
问题 I am tying to display a JS object of data using underscore templates. I can't seem to be able to work out how to drill through the object to get the country names or other date (for example tarrifType) and display it using my template. The object looks like this... var items = [ { "country": { "China": [ { "tarrifType": "China Pass", "fixLine": "23p", }, { "tarrifType": "Monthly plan", "fixLine": "3p", } ], "Australia": [ { "tarrifType": "Pay as you go", "fixLine": "73p" }, { "tarrifType":

Error: Cannot find module 'underscore' thrown in console for all Meteor commands

故事扮演 提交于 2019-12-22 09:16:05
问题 Error: Cannot find module 'underscore' thrown in console for all Meteor commands After every meteor or meteorite command such as meteor or mrt create myapp the following error is thrown. This error seemed to appear suddenly, as meteor was functional the night before and no changes have been made since. Austins-MacBook-Pro:Projects austinrivas$ mrt create test-app /Users/austinrivas/.meteor/tools/3cba50c44a/tools/meteor.js:1480 }).run(); ^ Error: Cannot find module 'underscore' at Function

Backbone.js Routers confusion (pushState: true, trailing slash)

谁都会走 提交于 2019-12-22 08:35:30
问题 I am using Backbone's router with pushState:true to handle my site's urls. Examples of URLs include: http://domain.com/John http://domain.com/John/ http://domain.com/John/photos http://domain.com/John/my-latest-photos-2012 Problem : When the user goes to http://domain.com/John/, the expected function photos is executed. However when the user goes to http://domain.com/John without the trailing slash, nothing happens; my guess is that the trailing backslash defined in root prevented this.

Filter array based on an array of index

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 08:08:56
问题 First I apologize if it's a duplicate (I searched but did not find this simple example...), but I want to select elements of arr1 based on the index in arr2 : arr1 = [33,66,77,8,99] arr2 = [2,0,3] I am using underscore.js but the 0 index is not retrieved (seems to be considered as false ): res = _.filter(arr1, function(value, index){ if(_.contains(arr2, index)){ return index; } }); Which returns: # [77, 8] How could I fix this, and is there a simpler way to filter using an array of indexes? I

Group array values in group of 3 objects in each array using underscore.js

喜你入骨 提交于 2019-12-22 07:54:34
问题 Below is an array in which I have to group 3 values in each object var xyz = {"name": ["hi","hello","when","test","then","that","now"]}; Output should be below array - ["hi","hello","when"]["test","then","that"]["now"] 回答1: Hi please refer this https://plnkr.co/edit/3LBcBoM7UP6BZuOiorKe?p=preview. for refrence Split javascript array in chunks using underscore.js using underscore you can do JS var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"]; var n

Place client-side JavaScript templates in HTML or JavaScript?

百般思念 提交于 2019-12-22 06:00:24
问题 Should client-side templates like the following (using underscore's templating engine): <p class="foo"><%= bar %></p> be placed in an separate HTML file, or a separate JavaScript file? I know it could work both ways. For example, a JavaScript file could just contain a list of string variables like so: var cute = '<p class="the"><%= unicorn %></p>'; var fb = '<p class="new-design"><%= sucks %></p>'; But I have also seen the following: <script type="text/template" id="omg-template"> <span id=

How can I add a delay inside each iteration of an _.each loop in underscore.js?

徘徊边缘 提交于 2019-12-22 05:40:52
问题 How can I add a delay inside each iteration of an _.each loop to space out the calling of an interior function by 1 second? _.each(this.rows, function (row, i) { row.setChars(msg[i] ? msg[i] : ' '); }); 回答1: You don't need extra IIFE _.each(this.rows, function (row, i) { setTimeout(function () { row.setChars(msg[i] ? msg[i] : ' '); }, 1000 * i); }); since you're not doing it in an explicit for loop. 回答2: Found an answer, just add a self invoking function inside the _.each loop with a timeout

Grouping objects by multiple columns with Lodash or Underscore

天涯浪子 提交于 2019-12-22 04:37:07
问题 I have following object records : { "notes":[ { "id":1, "description":"hey", "userId":2, "replyToId":null, "postId":2, "parentId":null }, { "id":5, "description":"hey test", "userId":3, "replyToId":null, "postId":2, "parentId":null }, { "id":2, "description":"how are you", "userId":null, "replyToId":2, "postId":2, "parentId":null, "user":null } ] } I want to output it as: 2 object with id 1 object with id 2 (because replyToId value is same as userId 3 object with id 5 So basically I want to