underscore.js

Is it possible to iterate through two lists at the same time in Underscore.js?

亡梦爱人 提交于 2019-12-05 17:26:59
问题 I basically want to express the following behavior using _.each() or _.map() in Underscore.js. a = [1, 2, 3] b = [3, 2, 1] # Result list c = [0, 0, 0] for i in [0 .. a.length - 1] c[i] = a[i] + b[i] This is definitely possible in Matlab (my primary language) as such: c = arrayfun(@(x,y) x+y, a, b) Intuitively, it feels like the syntax in Underscore should be: c = _.map(a, b, function(x, y){ return x + y;}) However, that argument list isn't acceptable; the second parameter is supposed to be a

jQuery or underscore Sorting a list by span text

独自空忆成欢 提交于 2019-12-05 16:49:30
I am a bit lost with trying to order an ul list by the span in the li. This is my html code: <ul id="appsList"> <li><span>aa</span> <span class="sort">android</span></li> <li><span>aa</span> <span class="sort">ios</span></li> <li><span>aa</span> <span class="sort">facebook</span></li> <li><span>bb</span> <span class="sort">android</span></li> <li><span>bb</span> <span class="sort">ios</span></li> <li><span>bb</span> <span class="sort">facebook</span></li> </ul> I have an array that holds the platform names and doesn't have to contain all the platforms and the order of the platforms in the

Binding and _.bindAll in backbone.js

不羁岁月 提交于 2019-12-05 16:40:33
I am confused about binding and the purpose of _bind.All in backbone.js. Below is a working code that creates a Modal view #modal and renders out comments fetched from the backend. Firstly, in the code below, I have in the initialize function _.bindAll(this, 'render', 'renderComments'); . Whether or not I do _.bindAll() , I have no problems calling this.render() and this.renderComments() inside initialize() . Are there any examples of when _.bindAll() will help us and when it will not? ModalView = Backbone.View.extend({ el: $('#modal'), template: _.template( $('#tpl_modal').html() ),

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

徘徊边缘 提交于 2019-12-05 15:47:25
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. Router var AppRouter = Backbone.Router.extend({ routes: { '': 'photos', 'photos': 'photos' }, viewing

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

徘徊边缘 提交于 2019-12-05 15:28:16
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.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require

Backbone/Underscore uniqueId() Odd Numbers

筅森魡賤 提交于 2019-12-05 14:30:29
I'm relatively new to Backbone and Underscore and have one of those questions that's not really an issue - just bugging me out of curiosity. I built a very simple app that allows you to add and remove models within a collection and renders them in the browser. It also has the ability to console.log the collection (so I can see my collection). Here's the weird thing: the ID's being generated are 1,3,5... and so on. Is there a reason specific to my code, or something to do with BB/US? Here's a working Fiddle: http://jsfiddle.net/ptagp/ And the code: App = (function(){ var AppModel = Backbone

Underscore.js to filter out the unique values

风格不统一 提交于 2019-12-05 13:22:27
Here is a link to my Fiddle I have a table that is created dynamically from the JSON obtained from a Ajax response. I have duplicate entries in the table. For example xxx,xxx . I want to group them and have it like <td data-count="some count">xxx</td> I tried using underscore.js but it seems to break the rowspan functionality. I converted the arr into a object using _countBy(arr) and I obtained an object of the following format. {xxx: 2, zzz: 1} Question : How can I modify the generateTable() function to accommodate this change. I tried to modify it the following way var i=0; $.each

Backbone and bindAll: “func is undefined”

谁说我不能喝 提交于 2019-12-05 13:19:14
I've got problems using bindAll. The error I get is func is undefined . Any thoughts on what I'm doing wrong? I've tried both bindAll (fails with the above error) and individual bind s (don't work) window.test = Backbone.View.extend({ collection: null initialize: -> console.log('initialize()') console.log(this) # _.bindAll(this, ["render", "foo"]) _.bind(this.render, this) # these don't throw errors, but binding won't work _.bind(this.foo, this) @collection = new Backbone.Collection() @collection.bind "add", @render @collection.bind "add", @foo @render() foo: -> # won't be bound to the view

IE 11 smooth scrolling not firing intermediate scroll events

旧街凉风 提交于 2019-12-05 12:24:55
问题 If we make a simple test case like: document.documentElement.addEventListener('scroll', function() { console.log(document.documentElement.scrollTop); }); And then go and scroll using the scrollbar by clicking the track, or by using PageDown/PageUp, then we can see that we only get one event at the end of the scrolling animation. Now theoretically I could fix some of that behaviour by simulating the scroll events. Example code with jQuery and Underscore: $(function () { var $document = $

What is the importance of breaker in underscore.js? [duplicate]

梦想的初衷 提交于 2019-12-05 12:07:01
This question already has an answer here : underscore's each checking for {} return of callback (1 answer) Closed 5 years ago . In underscore.js , there is code like this: if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return; for example in "each" function var each = _.each = _.forEach = function(obj, iterator, context) { if (obj == null) return; if (nativeForEach && obj.forEach === nativeForEach) { obj.forEach(iterator, context); } else if (obj.length === +obj.length) { for (var i = 0, length = obj.length; i < length; i++) { if (iterator.call(context, obj[i], i, obj) ===