underscore.js

The order of operators in JavaScript (|| ,&&)

故事扮演 提交于 2020-01-02 06:24:28
问题 I am reading the source code of Underscore.js, then something confused me: // Its code, check the passed-in parameter obj _.isObject = function(obj) { var type = typeof obj; return type === 'function' || type === 'object' && !!obj; }; I am confused about the operator order of expression. I think the operator precedence in return type === 'function' || type === 'object' && !!obj; will be from left to right ; I mean equal to : return (type === 'function' ) || ( type === 'object' && !!obj); if

what is it used for _.once in underscore?

风格不统一 提交于 2020-01-02 03:26:06
问题 I just look at the once API of source in underscore.js, then wandering what is it the used for in the method, it seems doing nothing: func = null the source: _.once = function(func) { var ran = false, memo; return function() { if (ran) return memo; ran = true; memo = func.apply(this, arguments); func = null; return memo; }; }; 回答1: What the function does can be found in the documentation: Creates a version of the function that can only be called one time. Repeated calls to the modified

Backbone collection fetch (add:true) does not update the collection

北城以北 提交于 2020-01-02 01:56:07
问题 loadMore: function(){ var $this = this; console.log(this.Messages); //SAME AS AFTER this.Messages.url = '/js/messages/?start=' + this.Messages.length this.Messages.fetch({'add':true, success:function(){ console.log($this.Messages); //SAME AS BEFORE?? }, error:function(){ } }); }, The collection is not updated. After this function, the events are fired, and the new items are drawn on the screen. The problem is that the collection did not add the new models. 回答1: As was mentioned in a previous

Reduce array of objects on key and sum value into array

二次信任 提交于 2020-01-01 10:28:21
问题 I have the following object: data = [ { name: 'foo', type: 'fizz', val: 9 }, { name: 'foo', type: 'buzz', val: 3 }, { name: 'bar', type: 'fizz', val: 4 }, { name: 'bar', type: 'buzz', val: 7 }, ]; And used lodash map: result = _.map(data, function item, idx){ return { key: item[key], values: item.value, } } Which results in: [ { key: foo, val: 9 }, { key: foo, val: 3 }, { key: bar, val: 4 }, { key: bar, val: 7 }, ] but now I'm trying to return: [ { key: 'foo', val: 12 }, { key: 'bar', val: 11

Testing a debounced function in AngularJS with Jasmine never calls the function

泪湿孤枕 提交于 2020-01-01 08:28:13
问题 I have a method in a service that uses underscore's debounce. Inside that method is a call to a method on a different service. I'm trying to test that the different service is called. In my attempts to test the debounced method, the different services' method is never called, and jasmine fails with: "Expected spy aMethod to have been called." I know for a fact that it IS called (it logs to console in chrome), it's just called AFTER the expectation already failed. So... (preferably) without

How to iterate an array synchronously using lodash, underscore or bluebird [closed]

☆樱花仙子☆ 提交于 2020-01-01 04:47:05
问题 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 have an array that contains filenames at each index. I want to download those files one at a time (synchronously). I know about ' Async ' module. But I want to know whether any functions in Lodash or Underscore or Bluebird library supports this functionality. 回答1: You can use bluebird's Promise

how to display names in alphabetical groups in Handlebarsjs underscorejs

牧云@^-^@ 提交于 2020-01-01 00:47:20
问题 Hi am New to Handlebarsjs. I have a collection of contacts with name, email, phone, etc. as below [ { "name": "Bob Wolmer", "email": "bob@wolmer.com", "phone": "(535) 235-1234", "address": "301 Cobblestone Wy., Berdrock 00000", "contactId": "1121", "labels": {} }, { "name": "Wilma Erra", "email": "wilma@erra.com", "phone": "(535) 235-3659", "address": "301 Cobblestone Wy., Berdrock 70777", "contactId": "1122", "labels": {} }, { "name": "Brad", "email": "brad@brad.com", "phone": "(535) 235

how to display names in alphabetical groups in Handlebarsjs underscorejs

孤街浪徒 提交于 2020-01-01 00:47:18
问题 Hi am New to Handlebarsjs. I have a collection of contacts with name, email, phone, etc. as below [ { "name": "Bob Wolmer", "email": "bob@wolmer.com", "phone": "(535) 235-1234", "address": "301 Cobblestone Wy., Berdrock 00000", "contactId": "1121", "labels": {} }, { "name": "Wilma Erra", "email": "wilma@erra.com", "phone": "(535) 235-3659", "address": "301 Cobblestone Wy., Berdrock 70777", "contactId": "1122", "labels": {} }, { "name": "Brad", "email": "brad@brad.com", "phone": "(535) 235

Gulp, html templates and fragment loading

夙愿已清 提交于 2019-12-30 23:42:49
问题 I want to import html fragments at various places in a generated html file when I run it through a gulp task. The following is my gulp task: gulp.task('build_html', function () { gulp.src('resources/index.html') .pipe(template({ident: '1'})) .pipe(gulp.dest('frontend')); }); Inside the index.html file is a lot of html I would like to load from fragments, for example a bootstrap dropdown menu <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

grunt-contrib-htmlmin how to ignore template tags

天大地大妈咪最大 提交于 2019-12-30 18:50:36
问题 I'm using grunt-contrib-htmlmin to minify my html in a backbone/underscorejs project, however, when I run grunt-contrib-htmlmin on any underscorejs template that has <%= myvar %>, the task outputs a parse error. Is there a way grunt-contrib-htmlmin can ignore text inside <%= and %>? 回答1: Since you posted this issue, an new feature was introduced into html-minifier (which is used by grunt-contrib-htmlmin ) to ignore the interpolation tags that were causing the problem. For instance, the