underscore.js

How to use groupBy in Backbone.js to group collections?

纵饮孤独 提交于 2019-12-21 01:28:07
问题 An example collection (showing models only): [ { name: "Bob" date: "Thu Mar 29 2012" }, { name: "James" date: "Fri Mar 30 2012" }, { name: "Dylan" date: "Fri Mar 30 2012" }, { name: "Stanley" date: "Sat Mar 31 2012" }, ] How can I use Underscore.js' groupBy function to group models with the same date? 回答1: Use _.groupBy(data, 'date'); You could also use pass a custom function, but in this case it's not necessary as the attribute shortcut syntax above works fine. _.groupBy(data, function(row)

Using loops in backbone/underscore's templates

半世苍凉 提交于 2019-12-20 16:30:32
问题 I have a backbone.js/underscore.js template that I am feeding into a backbone view for rendering. The View is passed a model that contains an array posts of objects (which I call post in the template). Problem : When I try to loop through all the elements of the array posts , I get an error Uncaught SyntaxError: Unexpected token ) and refers a line in the backbone View's code template: _.template( $('#tpl_SetView').html() ) . Am I doing the loop incorrectly which is causing this error?

backbone.js: overwritting toJSON

送分小仙女□ 提交于 2019-12-20 15:26:01
问题 I am trying to implement some sort of nested collections in a Backbone.Model To do this I have to overwrite the adapter functions which parse the server response and wrap the array into a collection and the function which serializes the whole object without any helper methods. I am having problems with the second one. var Model = Backbone.Model.extend({ urlRoot: "/model", idAttribute: "_id", // this wraps the arrays in the server response into a backbone collection parse: function(resp, xhr)

Underscore templating - changing token markers

元气小坏坏 提交于 2019-12-20 12:46:09
问题 Out of the box underscore templating uses the markers <%= %> for raw, and <%- %> for HTML escaped content. I know you can change the markers using something like: _.templateSettings.interpolate = /\{\{(.+?)\}\}/g; But how does this relate to raw and escaped content? It looks to me like you end up with only one type of marker. Or have I overlooked something? 回答1: The Underscore.js documentation says this (emphasis added): If ERB-style delimiters aren't your cup of tea, you can change

using memoize function with underscore.js

試著忘記壹切 提交于 2019-12-20 12:23:38
问题 I am trying to cache the result from an ajax call using memoize function from Underscore.js . I am not sure of my implementation. Also how to retrieve back the cached result data using the key. Below is my implementation: Javascript code: var cdata = $http .get(HOST_URL + "/v1/report/states") .success(function(data) { //put the result in the angularJs scope object. $scope.states = data; }); //store the result in the cache. var cachedResult = _.memoize( function() { return cdata; }, "states");

Get object keys for filtered values

假如想象 提交于 2019-12-20 11:07:25
问题 The case is simple - I've got a following object: Object {1: false, 2: true, 3: false, 4: false, 5: false, 6: false, 7: false, 8: true, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false} and I need to get an array of ids that had true value, using underscore. In above case that would be: [2, 8] I tried few things but I'm a bit stuck. Does anyone have any idea? 回答1: var keys = []; _.each( obj, function( val, key ) { if ( val ) { keys.push(key); } }); There

underscore.js - Is there a function that produces an array thats the difference of two arrays?

倖福魔咒の 提交于 2019-12-20 10:27:40
问题 Looking for a function in underscore.js that will take 2 arrays and return a new array of unique values? Something like _without _.without([0, 1, 3, 9], [1, 3]); I would like => [0,9] returned It appears _without's 2nd arg is a list of values, not an array. Anyone out there know if underscore has the specific function I'm looking for? Or can I take an exisitng array and covert it to values the function expects. Thanks, ~ck in San Diego 回答1: _.without.apply(_, [arr1].concat(arr2)) [[0, 1, 3, 9

How to cancel a debounced function after it is called and before it executes?

北城余情 提交于 2019-12-20 10:23:03
问题 I create a debounced version of a function with underscore: var debouncedThing = _.debounce(thing, 1000); Once debouncedThing is called... debouncedThing(); ...is there any way to cancel it, during the wait period before it actually executes? 回答1: If you use the last version of lodash you can simply do: // create debounce const debouncedThing = _.debounce(thing, 1000); // execute debounce, it will wait one second before executing thing debouncedThing(); // will cancel the execution of thing

Underscore.js - Map Array of key/value pairs to an Object - One liner

爷,独闯天下 提交于 2019-12-20 10:21:47
问题 I've been going through the underscore docs but I can't seem to find a method (or nested method call) to do the following transformation: Let's say I have the following Javascript array: [{ "name" : "sEcho", "value" : 1},{ "name" : "iColumns", "value" : 12}, ... ] And I need to transform it into the following object: { sEcho: 1, iColumns: 12, ... } I'm using underscore.js for a reason so it must be a one liner. 回答1: Since nobody has posted this as an answer, I'm posting it because I think it

Does the chain function in underscore.js create a monad?

白昼怎懂夜的黑 提交于 2019-12-20 10:00:29
问题 In the chain documentation you find: Calling chain on a wrapped object will cause all future method calls to return wrapped objects as well. When you've finished the computation, use value to retrieve the final value. So does the chain function create a monad? 回答1: No, not a monad, but a comonad! It turns a function that takes a wrapped object and returns a normal value into a function that both takes and returns a wrapped object. As a Haskell type signature that would be: (Wrapped a -> b) ->