underscore.js

Underscore.js filter not returning all results [duplicate]

别来无恙 提交于 2019-12-13 17:18:35
问题 This question already has answers here : Why does a RegExp with global flag give wrong results? (6 answers) Closed 2 years ago . I am trying to build an filtering function that works similar to the way an auto-complete field would work. I am using Underscore.js to filter an array of names. Here is a JSFiddle that demonstrates the problem: http://jsfiddle.net/PWcVM/ And for those who would rather not jump to JSFiddle, here is the example in plain text. HTML: <p>Type the name 'Craig' into the

underscore isEqual and JSON

末鹿安然 提交于 2019-12-13 16:26:24
问题 I have a question using underscore isEqual to compare two JSON strings. Currently i have done an app in backbone, and I'm using _.isEqual(savedModel.toJSON(),changedModel.toJSON() ) to detect if the model has changed in the page and promt a "You have unsaved changes, do you want to save?" dialog if the user tires to navigate away. For some reason I get the dialog in random places even though I have done nothing or have saved changes. Debugging is driving me crazy. Could this be because JSON

Single function call on scroll event?

独自空忆成欢 提交于 2019-12-13 16:14:33
问题 Question Up Front: How can I change this script so that a neat, single function call occurs for either an up or down scroll? Here's a JSFiddle which represents the problem clearly. The following script successfully pushes out an alert based on the direction of the user's scroll direction: //Firefox $('html').on('DOMMouseScroll', function(e){ var delta = e.originalEvent.detail; if (delta > 0) { alert('You scrolled up'); } else if (delta < 0) { alert('You scrolled down'); } }); //Everything

Change event triggering on momentjs object

随声附和 提交于 2019-12-13 15:11:37
问题 For one of my projects, i'm listening on attribute changes on a model object and calling view methods if its attributes change. Problem is one of the attribute of my model is a momentjs date object. I've looked into backbone source and it seems it triggers changes in the setter using underscore method _.isEqual() . After reading underscore documentation, isEqual does a deep comparison of both objects. Seems alright but momentjs object contains the initial formatting informations and even if

Finding A - B from two arrays using underscore.js

半世苍凉 提交于 2019-12-13 14:09:28
问题 I have to filter out certain elements from an array in javascript and thought of using underscore.js for this purpose. As I am new to it,some help is appreciated.Please refer the code below , I have to find A \ B and assign the result to C . Does underscore.js has any convinience method to do that ? function testUnderScore(){ alert("underscore test"); var a = [84, 99, 91, 65, 87, 55, 72, 68, 95, 42]; var b = [ 87, 55, 72,42 ,13]; var c = []; alert(c); } 回答1: By using difference method: var c

JQuery event inside a underscore.js template

99封情书 提交于 2019-12-13 14:07:00
问题 I'm having trouble including javascript code inside an underscore.js template (particularly a jquery .click() event). I've tried many variations of the <% .. %> tags, and here is my latest: <script type="text/html" id="eventTemplate"> <% _.each(words, function(word) { %> <a data-role="button" id="eventButton <%= word %>" href="#" data-icon="plus" data-iconpos="right"> <%= word %> </a> <% $('#eventButton' + word +').click(function() { console.log(word); }); }); %> Any help will be appreciated.

Creating a Calendar like table using underscore and backbone js templates

*爱你&永不变心* 提交于 2019-12-13 09:23:08
问题 Hi Guys I am a total noob at backbone and underscore. I need to create a table that shows appointment bookings for some dates. I have to make it look something like this: This is what my collection looks like: [ {"id":0, "startDate":"04/11/2013", "serviceID":241, "providerID":223, "timeSlots": ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"]}, {"id":0, "startDate":"05/11/2013", "serviceID":241, "providerID":223, "timeSlots": ["09:00","10:00","11:00","12:00","13:00","14:00",

How to change data array to be object using javascript?

馋奶兔 提交于 2019-12-13 08:49:17
问题 I have data like this {"black":["/file/d60f198e-dde5-4e5e-b2ab-1ee10c96a027.png"]} how to change data to be "black" : "/file/d60f198e-dde5-4e5e-b2ab-1ee10c96a027.png" ? I'm using javascript. Thanks advance 回答1: A generic solution would be something like: var data = { black: ['abc'], red: ['xyz'] } var result = _.mapObject(data, _.first) which would give you: { black: 'abc', red: 'xyz' } 回答2: Just set that property to be the value of the array: var original = {"black":["/file/d60f198e-dde5

Merge JSON object response into one using Underscore.js

﹥>﹥吖頭↗ 提交于 2019-12-13 07:25:43
问题 I'm trying to merge the following two JSON objects that are Youtube API response. I will likely have hundreds of videos (sets of #1 and #2) and I want them to be merged into one JSON in the following format. *Please ignore naming and syntax in the following, they are just for explanation here. Video-1: { a: {..}, b: {..} }, video-2: { a: {..}, b: {..} }, video-3: { a: {..}, b: {..} }, ... As long as the second response is appended to the first one in the set order, I'm happy. Not like {a, a,

How can I subtract one array from another? [duplicate]

风格不统一 提交于 2019-12-13 06:59:27
问题 This question already has answers here : using underscore's “difference” method on arrays of objects (8 answers) Closed 5 years ago . I have two arrays: var collectionA = [ {'name': 'Brandon', 'age': '41'} ]; var collectionB = [ {'name': 'Brandon', 'age': '41'}, {'name': 'Tom', 'age': '25'}, {'name': 'Jimmy', 'age': '36'}, {'name': 'Brian', 'age': '36'} ]; How can I get create a third array that is basically the result of subtracting collectionA from collectionB ? FYI: I'm using Underscore