underscore.js

Javascript: is there a better way to execute a function after x amount of async database/ajax calls

≡放荡痞女 提交于 2019-12-06 07:31:18
using Backbone.js we have an application, in which on a certain occasion we need to send an ajax post to a clients webservice. however, the content to be posted, is dynamic, and is decided by a certain array. for each item in the array we need to go fetch a piece of data. after assembling the data that aggregated object needs to be sent. as of now, i have a synchronous approach, though i feel that this is not the best way. var arrParams = [{id: 1, processed: false},{id: 7, processed: false},{id: 4, processed: false}]; function callback(data) { $.post()... // jquery ajax to post the data... }

unable to use create method on collections in backbonejs

守給你的承諾、 提交于 2019-12-06 07:26:45
I have a collection in backbone like this: app.ledgerList=Backbone.Collection.extend({ model: app.ledger, localStorage:new Store('ledgers') }); ///initializing app.ledgers=new app.ledgerList(); And in one of my views later in app, i have: saveLedger: function(){ name=$("#ledgerName").val(); email=$("#ledgerEmail").val(); address=$("#ledgerAddress").val(); phone=$("#ledgerPhone").val(); console.log(app.ledgers.length); app.ledgers.create({name:name,email:email,address:address,phone:phone}); } This returns me Uncaught TypeError: Object [object Object] has no method 'apply' on the line app

Backbone js - Uncaught type error: Cannot read property 'on' of undefined

倖福魔咒の 提交于 2019-12-06 06:29:35
I have the following a very simple ToDo List app using Backbone framework. <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>To do List</title> <style> .completed{ text-decoration: line-through; color: #666; } </style> <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script> <script type="text/javascript" src="js/underscore.js"></script> <script type="text/javascript" src="js/backbone.js"></script> </head> <body> <h1>My Todos</h1> <div id="tasks">

How do I escape EJS template code in node.js to be evaluated on the client side?

删除回忆录丶 提交于 2019-12-06 06:08:13
I use node.js/ejs on the server side and backbone.js on the client side. Both server side and client side use the same templating style. So the problem is, if I put template code meant for the client inside a template it still get's parsed on the server side. If found out that something like this works: <%- "<%= done ? 'done' : '' %\>" %> However, IMHO this uglifies the code in a way which makes the whole point of using templates useless. How would you approach this? Is there a way to define blocks of code inside EJS-templates which do not get parsed like a {literal}-tag used in other

Functional way of joining two js object arrays based on common id

a 夏天 提交于 2019-12-06 05:22:10
问题 I am trying to acheive something similar to SQL table join, in the most elegant (functional) way, preferably with underscore.js, so no for loops please. I need to merge objects from two different arrays, matched upon a common identifier. For example, given: var basic = [{ id: '1', name: 'someName', }, {...} ] var ext= [{ id: '1', job: 'someJob', }, {...} ] Result should be: var combined = [{ id: '1', name: 'someName', job: 'someJob', }, {...} ] Thanks! 回答1: Map, findWhere and extend should do

Print all paths from root node to leaf nodes - javascript

泪湿孤枕 提交于 2019-12-06 04:24:45
function formCategoryTrees(object) { _.each(object,function(objectValues){ var leafCategoryId = objectValues["id"]; var leafCategoryName = objectValues["name"]; console.log(leafCategoryName+""+leafCategoryId ); if (objectValues.hasOwnProperty("children")) { if (typeof objectValues["children"] == 'object') console.log("In"); formCategoryTrees(objectValues["children"]); }else{ console.log(leafCategoryName); } }) } So i want to display the category tree as follows: Mobile & Accessories -> Mobiles Mobile & Accessories -> Chargers My JS Fiddle: http://jsfiddle.net/tfa8n5tj/ I believe you want your

Break out of an _.each loop

会有一股神秘感。 提交于 2019-12-06 04:10:40
问题 Is it possible to break out of an underscore each loop..? _.each(obj, function(v,i){ if(i > 2){ break // <~ does not work } // some code here // ... }) Is there another design pattern I can be using? 回答1: I don't think you can, so you will just have to wrap the contents of the function in i < 2 or use return . It may make more sense to use .some or .every . EDIT: //pseudo break _.each(obj, function (v, i) { if (i <= 2) { // some code here // ... } }); The issue with the above is of course

jQuery equivalent of underscore.js' groupBy

偶尔善良 提交于 2019-12-06 03:07:08
问题 Is there a built in function in jQuery that does the equivalent of http://underscorejs.org/#groupBy ? Any workarounds? Thanks 回答1: No. jQuery was not made for data handling, but for DOM, Ajax and Animations - those utility functions as each , map or grep which are needed internally suck. Use Underscore, there is nothing wrong with it! If you don't want to load the whole script, you can easily copy the groupby function from the source to wherever you need it. Don't forget to add a comment on

How to solve _.some using _.every?

北城以北 提交于 2019-12-06 03:02:42
问题 Working on a programming challenge to re-implement the functionality of underscore.js in standard javascript. Specifically I am working on implementing the _.some function. (http://underscorejs.org/#some) The part I'm struggling with is it's asking me to find a way to solve it using _.every internally. (http://underscorejs.org/#every) I have already finished the _.every function earlier and it works as it should. Here is logically what I am wanting to do in sketched code: _.some = function

Are Vue.js and debounce (lodash/underscore) compatible?

妖精的绣舞 提交于 2019-12-06 02:18:06
问题 Following an answer to my question on debouncing I am wondering if vue.js and lodash / underscore are compatible for this functionality. The code in the answer var app = new Vue({ el: '#root', data: { message: '' }, methods: { len: _.debounce( function() { return this.message.length }, 150 // time ) } }) <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.6/vue.js"></script> <script src="https://unpkg.com/underscore@1.8.3"></script> <!-- undescore import --> <div id="root"> <input v