underscore.js

inheritance using underscore

大城市里の小女人 提交于 2019-12-06 14:26:04
问题 I've been given a class - Zoo.Controller = (function() { function Controller() {} Controller.prototype.params = {}; Controller.prototype.set_params = function(params) { this.params = params; return this; }; return Controller; })(); and I want to inherit from that class using _.extend Zoo.Controllers.WhaleController = _.extend({ new: function () { // do something } }, Zoo.Controller); When I try to instantiate that class like so... this.whale_controller = new Zoo.Controllers.WhaleController();

Best way to take out keys with invalid (NaN, blank, etc) values from an object?

微笑、不失礼 提交于 2019-12-06 14:05:30
I've got a short search form that a user fills out. There will be multiple search queries that will go into MongoDB: The form creates a variable called searchParams that may look like this: var searchParams = { city: "Springfield", bedrooms: 3, bathrooms: 2 }; I then have a function that takes the searchParams as an argument and queries Mongo using it: var searchListings = function(searchParams){ return db.MyListings.find(searchParams).fetch(); } db.MyListings.find( {city: "Springfield", bedrooms: 3, bathrooms: 2} ).fetch(); This is all well and good for a complete searchParams object, but if

Loading underscore.js templates from a separate file

六月ゝ 毕业季﹏ 提交于 2019-12-06 12:59:05
I'm currently using underscore.js for templating in a project, templates are stored in script tags with a type of text/template and loaded by id. I'm wondering if it's possible to continue to use the same system, but move the templates to a separate file? The only way I can think about doing this is declaring the templates as global vars in a separate file, but that seems ugly. Note: I don't want to use Jammit or some other build system for mashing everything together into a single file at deployment time, wondering if there's another solution. I personally use RequireJS to load my templates

What exactly does .apply do?

一世执手 提交于 2019-12-06 12:46:06
Never seen the .apply method before. Can someone explains to me what it does? This is taken from http://addyosmani.github.com/backbone-fundamentals/ var app = app || {}; var TodoList = Backbone.Collection.extend({ model: app.Todo, localStorage: new Backbone.LocalStorage(’todos-backbone’), completed: function() { return this.filter(function( todo ) { return todo.get(’completed’); }); }, remaining: function() { return this.without.apply( this, this.completed() ); }, nextOrder: function() { if ( !this.length ) { return 1; } return this.last().get(’order’) + 1; }, comparator: function( todo ) {

How to evaluate javascript functions in underscore with mustache notation?

不问归期 提交于 2019-12-06 12:24:30
I want to do something like this: <script id="tmpl-books" type="text/template"> <ul> <% for (var i = 0; i < books.length; i++) { %> <% var book = books[i]; %> <li> <em><%= book.title %></em> by <%= book.author %> </li> <% } %> </ul> </script> in my code, but I am using JSP so I have to use the {{ }} notation, but when I do this {{ for(var... }} does not work. <script id="tmpl-books" type="text/template"> <ul> {{ for (var i = 0; i < books.length; i++) { }} <% var book = books[i]; %> <li> <em>{{= book.title }}</em> by {{ book.author }} </li> {{ } }} </ul> </script> How can I achieve this? mu is

JavaScript对象的长度

巧了我就是萌 提交于 2019-12-06 10:31:04
我有一个JavaScript对象,是否有内置的或公认的最佳实践方法来获取此对象的长度? const myObject = new Object(); myObject["firstname"] = "Gareth"; myObject["lastname"] = "Simpson"; myObject["age"] = 21; #1楼 更新 :如果您使用的是 Underscore.js (建议使用,它是轻量级的!),那么您可以 _.size({one : 1, two : 2, three : 3}); => 3 如果不是 ,并且您不想由于任何原因弄乱Object属性,并且已经在使用jQuery,那么同样可以访问一个插件: $.assocArraySize = function(obj) { // http://stackoverflow.com/a/6700/11236 var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) size++; } return size; }; #2楼 以下是James Coglan在CoffeeScript中针对那些放弃了直接JavaScript的人的答案:) Object.size = (obj) -> size = 0 size++ for own key of

Prevent tab contents from loading multiple times (backbone.js)

好久不见. 提交于 2019-12-06 10:06:51
I have a few tabs on my page, whose contents (consisting of many SetView contained by SetListView ) are loaded using Backbone.js whenever its tabs is being clicked on. Problem:: When the user switches from a tab to a previously loaded/viewed tabbed, the contents load again and append to the previously loaded content in SetListView . I can get it to clear the previously loaded contents before loading it again, but it seems to be less than optimal to keep loading the same content. Is it possible to make Backbone.js store existing content for a tab and not load it multiple times when switching

Convert array of objects to object of arrays using lodash

别来无恙 提交于 2019-12-06 09:44:20
The title is a little bit confusing but I essentially want to convert this: [ {a: 1, b: 2, c:3}, {a: 4, b: 5, c:6}, {a: 7, b: 8, c:9} ] into: { a: [1,4,7], b: [2,5,8], c: [3,6,9] } using lodash (requirement). Any ideas??? Here's a solution using lodash that maps across the keys and plucks the values for each key from the data before finally using _.zipOobject to build the result. var keys = _.keys(data[0]); var result = _.zipObject(keys, _.map(keys, key => _.map(data, key))); Look for _.map here input = [ {a: 1, b: 2, c:3}, {a: 4, b: 5, c:6}, {a: 7, b: 8, c:9} ]; output = {}; _.map(input,

How to re-create Underscore.js _.reduce method?

三世轮回 提交于 2019-12-06 09:11:47
For education purposes, I was trying to re-create Underscore.js's _.reduce() method. While I was able to do this in an explicit style using for loops. But this is far from ideal because it mutates the original list that was supplied as an argument, which is dangerous. I also realized that creating such method using functional programming style is harder, since it is not possible to explicitly set i value for looping. // Explicit style var reduce = function(list, iteratee, initial) { if (Array.isArray(list)) { var start; if (arguments.length === 3) { start = initial; for (var i = 0; i < list

Underscore's similar functions: _.contains vs. _.some and _.map vs _.each

荒凉一梦 提交于 2019-12-06 08:17:32
问题 Is there a reason to use one over the other? It seems that _.some and _.map are easier to use or applicable to more situations (from my very limited experience) but from reading it, they sound as if they should do the same thing. I'm sure there are other instances of this and I'm all ears on learning some of the comparisons. 回答1: _.contains vs _.some _.contains (_.contains(list, value, [fromIndex]) Returns true if the value is present in the list. Uses indexOf internally, if list is an Array.