underscore.js

Combine/merge objects in array based upon common value

被刻印的时光 ゝ 提交于 2019-12-07 16:10:40
问题 I have an array like the following. [ { sku: 'TEA-BLCK', price: '10', quantity: '1' }, { sku: 'TEA-ELGY', price: '10', quantity: '1' }, { sku: 'TEA-CHAI', price: '10', quantity: '1' }, { sku: 'TEA-GREN', price: '10', quantity: '1' }, { sku: 'TEA-ELGY', price: '10', quantity: '1' }, { sku: 'TEA-MINT', price: '10', quantity: '1' } ] I need to make it look like this [ { sku: 'TEA-BLCK', price: '10', quantity: '1' }, { sku: 'TEA-ELGY', price: '10', quantity: '2' }, { sku: 'TEA-CHAI', price: '10',

Understanding the declaration of the underscore in _.js?

依然范特西╮ 提交于 2019-12-07 16:02:13
问题 This is from the beginning of the annotated source of _.js. Try though I may, my JavaScript abilities are not at a high enough level to understand what's going on here. I'm hoping someone can give a real step by step explanation. I really have literally no idea what the code below does besides somehow setting up the _ for use, despite that I understand each individual expression. var _ = function(obj) { if (obj instanceof _) return obj; if (!(this instanceof _)) return new _(obj); this.

Pass exisiting Object from Parse Class otherwise create a new object

余生颓废 提交于 2019-12-07 15:37:29
As of right now I have a beforeSave function that prevents duplicate emails from being saved shown below. Parse.Cloud.beforeSave("Email", function(request, response) { var query = new Parse.Query("Email"); // Gets the email key value (string) before object is saved query.equalTo("address", request.object.get("address")); // Checks to see if an object for that email already exists query.first({ success: function(object) { if (object) { response.error("Email already Exists"); } else { response.success(); } }, error: function(error) { response.error("Could not determine if this email exists"); }

jQuery / underscore.js templating - Compatible with IE 6+?

余生长醉 提交于 2019-12-07 14:44:10
问题 So, I just learned about these recently and am about to put them into a site. Now, the question I am posing is, does this at all throw any kid of errors in IE 6+? Yes, I know IE6, what can you do.. When I say errors, I mean IE errors / warnings. Appreciate all the help as usual, thanks in advance. -Wes 回答1: Firstly, yes: jQuery does work with IE6 -- it says as much on the jQuery Homepage (hover over the "Cross browser" link and you'll see a popup that says IE6+). (having said that, a lot of

jQuery or underscore Sorting a list by span text

耗尽温柔 提交于 2019-12-07 09:21:06
问题 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

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

给你一囗甜甜゛ 提交于 2019-12-07 09:18:16
问题 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) {

Backbone and bindAll: “func is undefined”

隐身守侯 提交于 2019-12-07 08:06:52
问题 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()

Why can't I use Backbone.Collection as a generic collection? Is there an implementation that does this?

孤者浪人 提交于 2019-12-07 07:47:54
问题 After I asked this question and realized that Backbone.Collection s are strictly for Backbone.Models , I'm a little disappointed. What I was hoping for: Make underscore's methods more object oriented: _.invoke(myCollection, 'method'); ==> myCollection.invoke('method'); I'll admit, minor difference, yet still it seems nice. What problems will I run into if I use Backbone.Collection for non- Backbone.Models ? Are there any existing implementations, or a simple way to make a generic underscore

Uncaught TypeError: Cannot call method 'replace' of null

不打扰是莪最后的温柔 提交于 2019-12-07 06:30:33
问题 If I type "_.template($('#pranks-list').html())" on Chrome JS console it's works as well >> _.template($('#pranks-list').html()) function (a){return e.call(this,a,b)} app.js // Views window.PranksListView = Backbone.View.extend({ template: _.template($('#pranks-list').html()) }); Index.html <script type="text/html" id="pranks-list"> <li><a href='#pranks/<%= id %>'><%= name %></a></li> </script> </body> Why I get this error on this line?? template: _.template($('#pranks-list').html()) 回答1: It

Underscore bind vs jQuery.proxy vs Native bind

半世苍凉 提交于 2019-12-07 03:37:25
问题 I have some context issues in callback. I googled and found few option: Native bind - not supported by old browsers JQuery proxy underscore bind I'll definitely use native bind if I don't have to support old browsers. Is there any significant difference between these one should be aware of? Could these be used as an alternate to call/apply? 回答1: The call and apply methods invoke a function. The bind method returns a function object that can be used a reference (for use in callbacks, for