underscore.js

Underscore Conditionals in Template

岁酱吖の 提交于 2019-12-11 10:39:56
问题 Here is my template: var tmpl = _.template('<<%= elementTag %> <% if (_.has(elementClass)) { %> class="<%= elementClass %>" <% } %> <%= elementExtra %>><%= template(elementContent) %></<%= elementTag %>>'); Some of the objects that I will render using this template my not have an elementClass attribute set. I've tried to use _.has so that the template only attempts to print the attribute for objects where elementClass is defined but I have had no success. The console error states only that

Underscore.js - without and difference methods trouble

徘徊边缘 提交于 2019-12-11 10:19:03
问题 I am trying to use the _.without and _.difference methods in the underscore.js library with no luck. Here is some code. var someIds = _.pluck(parsedID1, 'id'); var otherIds = _.pluck(parsedID2, 'id); Here is where the trouble starts, I want to remove all of the otherIds out of the someId's list. So I have two options according to the website, either I can use _.difference || _.without. I use them like so... var newArray = _.without(_.toArray(someIds), _.toArray(otherIds)); I have also tried

filtering JSON using underscore

守給你的承諾、 提交于 2019-12-11 10:09:55
问题 I got a JSON name collection like this: [{ "name":"Jim", "prize":"Nokia Mobile" }, { "name":"Marry", "prize":"iPhone" }, { "name":"Alice", "prize":"iPad" } ] now I'd like to filter this JSON by a string: var data= "m"; so, this is what I've done: var data= "m"; collection = _.filter(collection,function(c){ return _.any(c,function(k){ return k.name.toLowerCase().indexOf(data.toLowerCase()) != -1; }); }); so, in this case, collection return elements which has m in their name. However, I've got

Underscore - Compare two arrays of objects (positions)

瘦欲@ 提交于 2019-12-11 09:57:35
问题 Is there a way to compare differences between arrays based on changes on their elements positions? I have an original array of objects which undergoes a change on one of it's element's values, this change is mapped into a new array: origElements = [{id: 1, value: 50}, {id: 2, value: 60}, {id: 3, value: 70}] changedElements = [{id: 1, value: 50}, {id: 3, value: 60}, {id: 2, value: 120}] var diff = _.difference(_.pluck(origElements, "id"), _.pluck(changedElements, "id")); var result = _.filter

How can accomplish For-Each loop from the underscore template to mustache

大憨熊 提交于 2019-12-11 09:43:23
问题 I have an underscore template and I have to use Mustache to render it. Below is my underscore template: <div id="sub-account"> <p>something</p> <table> <tr><td>Name</td> </tr> <tbody> <% _.each(accountList, function(account) { %> <tr> <td><%= account.get('name') %></td> </tr> <% }) %> </tbody> </table> </div> Im using a mustache as my main view to render a list. How can I loop through the code to render as a mustache template? Backbone Collection: var SubAccountCollection = Backbone

How can I import a file full of Underscore templates?

爷,独闯天下 提交于 2019-12-11 09:23:38
问题 As is in any project expected, 90% of my main HTML file is composed of many different templates, just like this: <script type="text/template" id="template-parametros"> <div id="parametros"> <h2>Parâmetros</h2> <table id="tab-parametros"> <tr><td>Descrição</td><td>Tipo</td><td>Locale</td></tr> </table> <button id="parametros-ad">Adicionar</button> </div> </script> I'd like to put them elsewhere, so the UX guys can work on them on their own. Putting them in another file is easy, but how can I

Group by Javascript Object keys + array

空扰寡人 提交于 2019-12-11 08:28:41
问题 I am trying to use underscore to group my array of objects in a smaller list: var list = [ { Region: 'A', Vals: [ 7, 'H' ] }, { Region: 'B', Vals: [ 40, 'H' ] }, { Region: 'B', Vals: [ 24, 'VH' ] }, { Region: 'C', Vals: [ 4, 'VH' ] }, { Region: 'A',Vals: [ 40, 'VH' ] } ]; var groups = _.groupBy(list, function(value){ return value.Region; }); var grouped = _.map(groups, function(group){ return { Region: group[0].Region, Vals: group[0].Vals } }); Which will get somewhat close, but it is missing

Javascript/Underscore, turn array of booleans into array of strings

孤街浪徒 提交于 2019-12-11 08:27:21
问题 So I am trying to accomplish turning an array of booleans to an array of strings, (only of the booleans that were set to true). This can be in either javascript, or underscore. Let me show you what I mean. I have an array like this : [{"item1" : [{"one": true, "two": false}]}, {"item2" : [{"one": false, "two": true}]}]; And the end result I am looking for is : [{"item1" : ["one"]}, {"item2" : ["two"]}]; It's worth mentioning, all of these keys will be dynamic. I can't seem to figuire out how

How do i pass a received array into underscore template

我与影子孤独终老i 提交于 2019-12-11 08:02:01
问题 I am getting a long array from PHP containing various data objects. [{"commid":"1","uid":"0","pid":"3","comment":"comm","parid":null,"date":"2016-10-27 15:03:10"}, {"commid":"2","uid":"0","pid":"10","comment":"Ana","parid":null,"date":"2016-10-27 15:03:51"}, {"commid":"3","uid":"0","pid":"5","comment":"asss!","parid":null,"date":"2016-10-27 15:05:50"}, {"commid":"4","uid":"0","pid":"10","comment":"Lawl?","parid":null,"date":"2016-10-27 17:03:59"}, {"commid":"5","uid":"0","pid":"14","comment":

Underscore Template Looping, Without A Loop?

[亡魂溺海] 提交于 2019-12-11 07:44:52
问题 This sort of follows on from another question I asked but the guy I think was trying to explain something else, or more likely I did not explain my self right. So all my backbone code is working, and displaying out the output right data. Now for this explain with my test data, I have 17 rows of inputs, but even without making a _.each loop, its looping around 17 times, which I just don't understand why? So my Backbone code, var TimeSheetModel = Backbone.Model.extend({ defaults: { Timesheetrow