underscore.js

Backbone.js Uncaught ReferenceError

僤鯓⒐⒋嵵緔 提交于 2019-12-13 06:20:25
问题 Im new to backbone, trying to fetch json from the Wordpress json plugin and render into a template, but when looping through posts I get the following error Uncaught ReferenceError: posts is not defined Any help appreciated. Thanks... jQuery(function($) { var Post = Backbone.Model.extend(); var Posts = Backbone.Collection.extend({ model: Post, url: '/api/get_post/?json=get_recent_posts', parse: function(resp) { console.log("posts", resp); return resp; } }); var listView = Backbone.View.extend

Meteor: Underscore _findWhere iteration through loop objects only works in chrome console, in app it says undefined

拥有回忆 提交于 2019-12-13 06:11:00
问题 I'm trying to fetch an object 'single Post' within an object 'Posts' from a json file within meteor, which looks like this. I found an effective way of doing it, using underscore findWhere to get to it. this is the code _.findWhere(_.findWhere(CategoryCollection.find().fetch(), {"_id":"CategoryPublication-5"}).posts,{"ID":46}); however when i put this into meteor, i'm getting undefined this is the code i used Template.CategoryArticleSingle.helpers({ articles: function () { var id = FlowRouter

JST with Backbone and Underscore

半城伤御伤魂 提交于 2019-12-13 06:08:24
问题 I am using Backbone and Underscore to create a small test site. I am compiling all my html template files into one JST javascript file as suggested here and here. It isn't however very obvious how to use this with template files. I have tried this: App.HeaderView = Backbone.View.extend({ el: '#headerContent', template: JST['header.html'](), //template: window["JST"]["header.html"], //template: _.template("<h1>Some text</h1>"), initialize: function () { this.render(); }, render: function() { /

Plucking an Object in an array using underscore.js

做~自己de王妃 提交于 2019-12-13 05:11:40
问题 Hi i had an array with n number of objects. I want to pluck the last but one object every-time. For example i had 4 objects in an array. I want the third object to be plucked using underscore.jS. Every time i want the last but one object to be plucked out of an array. Thanks in advance 回答1: You really don't need a library for this. var ary = ['thing1', 'thing2', 'thing3', 'thing4']; var aryLength = ary.length; var almostLast = ary[aryLength - 2]; This works for any array, not just this

UnderscoreJS wont extend object

狂风中的少年 提交于 2019-12-13 04:53:12
问题 I am trying to merge 2 objects using underscore. The destination object is a mongoose model but I have applied lean() to it to make it return a javascript object rather than a mongo document. model.find({}).lean().exec(function (error, object) {}); I then try extending using underscore _.extend(object, source); But it only returns the source object. I have tried with simple objects to test and those worked fine, so I am assuming it has something to do with mongoose? The simple objects that

Underscore check previous record in each

爷,独闯天下 提交于 2019-12-13 04:36:39
问题 I have an app in backbone and a template with underscore. I want to know if is possible to check a value inside a each of the previous record or something to check. Suppose that I have record like this: { id: 1, level:1, jump_level:2 }, { id: 2, level:2, jump_level:0 } Into my each I want to check if previous record has the same jump_level of the actual level because I want to tell that if i have to not print next record. This is a piece of my template: <div> <% _.each(room2, function(room) {

Filter two different structured arrays underscore js

寵の児 提交于 2019-12-13 04:34:31
问题 I have two arrays: array1 = [{Name: 'abc', ID: 23},{Name:'xyz', ID: 10},{Name:'def', ID: 12}]; array2 = [10,23]; Resultant array should be part of array 1 whose ID intersects with contents of array2. Here, the result would be result = [{Name: 'abc', ID: 23},{Name: 'xyz', ID:10}] ; Any ideas how I could achieve this using underscore js? 回答1: _.filter(array1, function(item){ return _.contains(array2, item.ID); }); You can use filter and contains. Try it out here! 回答2: Assuming your IDs are

Transform string of objects into elements of object

醉酒当歌 提交于 2019-12-13 03:13:37
问题 I have this below string of objects as shown below. [0,5] 0: "A123 G 2323232" 1: "F345 G 345667" 2: "T677 G -34343" 3: "G454 G 4343" 4: "" As you can see "A123 G 2323232" is a string which has tab seperated values in it. I would like to have a final output as follows. [0,4] 0: UserId:A123 Type: G Values: 2323232 1: UserId: F345 Type: G Values: 345667 2: UserId: T677 Type: G Values: -34343 3: UserId: G454 Type: G Values: 4343 Please note. the 4th element is empty string. so it should not

Rebuilding _zip in Javascript using map, arbitrary arguments

痴心易碎 提交于 2019-12-13 02:37:10
问题 I'm trying to learn JavaScript well and am practicing rebuilding some underscore functions. I'm trying to rebuild zip using map where there is an arbitrary number of arguments. Here is the solution I came up with, with pluck. I know that the underscore implementation itself uses _pluck, which internally uses Map, so I wanted to see if it was possible to do this with map... _.zip = function() { var argumentsArray = Array.prototype.slice.call(arguments); var longestArray = argumentsArray.sort

Underscore.js and noconflict

风格不统一 提交于 2019-12-13 02:34:18
问题 Writing a third-party javascript code which uses underscore.js and tries to avoid conflicts with the underscore.js version that might be used on the main site. So, my naive code is like this: window.$MyUS = _.noConflict(); I use $MyUS instead of _ from now on. Unfortunately, on some sites I receive the following error: Uncaught ReferenceError: _ is not defined What am I doing wrong? 回答1: There was a global exports object defined, which prevented the underscore.js to be installed properly. The