underscore.js

Binding a callback in Backbone.js and Underscore.js

点点圈 提交于 2019-12-10 20:23:16
问题 I have the following code: initialize: function() { _.bindAll(this); var callBack = function(res) { window.item = new Item(res); this.render(); }; _.bind(callBack, this); $.get('/item/parse', { uri: decodeURIComponent($.urlParam('uri')), title: decodeURIComponent($.urlParam('title')) }, callBack ); }, The intention is that render() should be called after the $.get function finishes. However, even after binding the callback function with _.bind, I still get "Object has no function render" in

filtering for max value in collection in backbone.js

丶灬走出姿态 提交于 2019-12-10 18:53:58
问题 I have a collection of models having structure: {"name":"xyz","company":"abc","recNumber":"M34/14-15/23"} I need to get that specific model from this collection whose recNumber is highest(based on number after last slash). I guess it can be done with .filter from underscore but don't know what will be the exact construct in this case. 回答1: try this; model = collection.max(function(m){ return _.last(m.get('recNumber').split('/')); }); collection.max() Returns the maximum value in list. _.last

How to get duplicates in a JavaScript Array using Underscore

不羁岁月 提交于 2019-12-10 18:52:17
问题 I have an array for which I need to the items that are duplicates and print the items based on a specific property. I know how to get the unique items using underscore.js but I need to find the duplicates instead of the unique values var somevalue=[{name:"john",country:"spain"},{name:"jane",country:"spain"},{name:"john",country:"italy"},{name:"marry",country:"spain"}] var uniqueList = _.uniq(somevalue, function (item) { return item.name; }) This returns: [{name:"jane",country:"spain"},{name:

Sort array of objects by value using underscore.js [closed]

江枫思渺然 提交于 2019-12-10 18:44:47
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I'm trying to sort an array of objects by the average property in descending order - so the largest average is first - but am not able to using underscore.js. Below is my attempt: var jsonData = [ { "title": "Dear Kitten", "totalCount": 1689, "average": 241 }, { "title": "Weird Things All Couples

Spec for async functions using Jasmine

不问归期 提交于 2019-12-10 17:46:06
问题 When I'm trying to test _.debounce function like in this qunit test using jasmine something strange happens. Seems like it could be tested using jasmine.Clock.useMock() ... But when I write: it('_.debounce()', function () { var spy = jasmine.createSpy('debounce'), debouncedSpy = _.debounce(spy, 100); jasmine.Clock.useMock(); // direct calls debouncedSpy(); debouncedSpy(); debouncedSpy(); // timed out calls setTimeout(debouncedSpy, 60); setTimeout(debouncedSpy, 120); setTimeout(debouncedSpy,

Remove value from array in Underscore

隐身守侯 提交于 2019-12-10 17:22:32
问题 Why isn't there a super simple remove function in underscore? var arr = [1,2,3,4,5]; _.remove(arr, 5); Sure I could use reject or without ... but neither of them are destructive. Am I missing something? I guess I'll do it the old fashioned way... arr.splice(arr.indexOf(5), 1); ugh 回答1: Explanation This is because Underscore provides functional concepts to JavaScript, and one of the key concepts of functional programming is Referential Transparency. Essentially, functions do not have side

Which Objects in JavaScript have a .length property? (aka Why does Underscore _.each treat my Function Object like an Array?)

蓝咒 提交于 2019-12-10 15:53:50
问题 I've been under the impression that only Array objects have a .length property. But, then again, I've also seen mentions of objects that are "array-like". I've not looked into this, and now it seems like my ignorance of this topic in JS may be biting me in the ass. Case in point: I've got the following code: var View = function(options) { // code }; _.extend(View, Backbone.Events, { make_children: function(parent) { // code } }); Later on, I use this View Function with Underscore's _.each ,

Replace in array using lodash

左心房为你撑大大i 提交于 2019-12-10 14:40:41
问题 Is there an easy way to replace all appearances of an primitive in an array with another one. So that ['a', 'b', 'a', 'c'] would become ['x', 'b', 'x', 'c'] when replacing a with x . I'm aware that this can be done with a map function, but I wonder if have overlooked a simpler way. 回答1: In the specific case of strings your example has, you can do it natively with: myArr.join(",").replace(/a/g,"x").split(","); Where "," is some string that doesn't appear in the array. That said, I don't see

How to use a variable for a property name when using underscore.js findWhere function?

谁说胖子不能爱 提交于 2019-12-10 13:53:47
问题 I'm having trouble figuring out how to set a property dynamically in underscore.js while using the _.findWhere function. Here's the documentation for the function: findWhere_.findWhere(list, properties) Looks through the list and returns the first value that matches all of the key-value pairs listed in properties. If no match is found, or if list is empty, undefined will be returned. _.findWhere(publicServicePulitzers, {newsroom: "The New York Times"}); => {year: 1918, newsroom: "The New York

Sort by object key in descending order on Javascript/underscore

老子叫甜甜 提交于 2019-12-10 13:49:24
问题 I have the following object array where the key is the date in UTC format. Array = [{1436796000000:["Task1","Task2"], 1437400800000:["Task4","Task8"], 1436968800000: ["Task3","Task2"], 1436882400000:["Task5","Task6"]}] I want to sort this array object by key in descending order. So the expected output will be following like the latest date will come first. Array = [{1437400800000:["Task4","Task8"], 1436968800000: ["Task3","Task2"], 1436882400000:["Task5","Task6"], 1436796000000:["Task1",