underscore.js

How to read recursively an object which looks like that

若如初见. 提交于 2019-12-08 05:44:30
问题 I have the following object: var xhr = JSON.parse('{"name1":{"errors":["This value should not be blank."]}, "children":{"name2":{"errors":["This value should not be blank."]},"message":[],"name3":{"errors":["This value should not be blank."]}, "children":{"name4":{"errors":["This value should not be blank."]} }}}'); console.log(xhr); I need to read recursively the xhr object. The object I posted is just an example, it means that the children could be more or less. Anywas objectReader should

Invalid web service call, missing value for parameter with Backbone and Webservice webmethods

我怕爱的太早我们不能终老 提交于 2019-12-08 04:39:23
问题 Is it possible for backbone to interface with asp.net soap webservice methods for saving and retrieving the data? because i got this error from the webmethod but actually the POST contains the parameters. Server Side [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static Dummy SaveDummy(Dummy myDummy) { Dummy dumdum = myDummy; HttpContext.Current.Session["data"] = dumdum; return myDummy; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet =

flatten javascript array of objects

孤人 提交于 2019-12-08 02:16:25
I have an array of object with hierarchical structure, something like this: [ {name: 'ParentOne', children: [ {name: 'ParentOneChildOne'}, {name: 'ParentOneChildTwo', children: [ {name: 'ParentOneChildTwoGrandChildOne'}, ]}, ]}, {name: 'ParentTwo', children: [ {name: 'ParentTwoChildOne', children: [ {name: 'ParentTwoChildOneGrandChildOne'}, {name: 'ParentTwoChildOneGrandChildTwo'} ]}, {name: 'ParentTwoChildTwo'} ]} ]; I want to flatten it: [ {name: 'ParentOne'}, {name: 'ParentOneChildOne'}, {name: 'ParentOneChildTwo'}, {name: 'ParentOneChildTwoGrandChildOne'}, {name: 'ParentTwo'}, {name:

unable to use create method on collections in backbonejs

大憨熊 提交于 2019-12-08 01:57:02
问题 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}); }

In an array of objects, how can I aggregate based on object property?

穿精又带淫゛_ 提交于 2019-12-07 20:25:12
问题 Say I have the following array of objects: dataArray = [ { id: "a", score: 1 }, { id: "b", score: 2 }, { id: "c", score: 5 }, ... { id: "a", score: 3 }, ... { id: "c", score: 2}, ... ] How can I obtain a resultArray like the following: resultArray = [ { id: "a", score: sum of all the scores when id is a }, { id: "b", score: sum of all the scores when id is b }, ... ... ] 回答1: If you use the underscore library: _.map _.groupBy(dataArray, 'id'), (v, k) -> {id: k, score: _.reduce(v, ((m, i) -> m

Loading underscore.js templates from a separate file

随声附和 提交于 2019-12-07 19:31:31
问题 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

How to get the unique keys of an array of object by underscore?

大憨熊 提交于 2019-12-07 19:09:09
问题 I have an array of object, and want to get the unique keys by underscore, how can I do that? Array[ Object{a: 1, b: 2}, Object{b: 3, c: 4, d: 5} ] I want to get: Array[ "a", "b", "c", "d" ] 回答1: Underscore solution: _.chain(xs).map(_.keys).flatten().unique().value(); Demo: http://jsbin.com/vagup/1/edit 回答2: Another option: keys = _.keys(_.extend.apply({}, array)) 回答3: Pure ES6 solution using Spread operator. Object.keys({...objA, ...objB}); Background It takes the properties of the items (

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

大兔子大兔子 提交于 2019-12-07 18:59:42
问题 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},

Uncaught TypeError: Cannot read property 'localStorage' of undefined

允我心安 提交于 2019-12-07 18:12:42
问题 I have the following in the backbonejs app: ////MODEL var app={}; app.ledger=Backbone.Model.extend(); ////COLLECTION app.ledgerList=Backbone.Collection.extend({ model: app.ledger, localStorage:new Store('ledgerstore') }); ////INITIATING COLLECTION app.ledgerlist=new app.ledgerList(); In one of my views i try to create a new model in the collection like this: newAttributes:function(){ return{ name:$("#ledgerName").val(), email: $("#ledgerEmail").val(), address: $("#ledgerAddress").val(), phone

Print all paths from root node to leaf nodes - javascript

被刻印的时光 ゝ 提交于 2019-12-07 16:39:33
问题 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 ->