underscore.js

Backbone.js collection and sortedIndex always returning 0

自作多情 提交于 2019-12-12 03:05:51
问题 I'm having trouble getting the sortedIndex underscore method to return a useful value. I have a collection with a comparator, and that's adding models correctly in order. I would just like to know the potential index of a new model, and the sortedIndex method is return 0 matter what I try. var Chapter = Backbone.Model; var chapters = new Backbone.Collection; chapters.comparator = function(chapter) { return chapter.get("page"); }; chapters.add(new Chapter({page: 9, title: "The End"}));

Sort Map/Hash by values, retaining keys

↘锁芯ラ 提交于 2019-12-12 02:49:27
问题 I am having a hell of a time finding an answer to this one. I would like to have a hash in JavaScript, like so: const hash = { a: 'A', c: 'C', b: 'B' } and I would like to sort the object by the value , such that it might become: const hash = { a: 'A', b: 'B', c: 'C', } now, I realize that POJSOs don't guarantee the order of their keys, so I was thinking of using immutableJS or some object that will guarantee the order of the keys in the hash. so I am looking for something like: var newHash =

Post form to an API with javascript and jquery

巧了我就是萌 提交于 2019-12-12 02:34:46
问题 Im not good with forms. How do you send a post form from an underscore.js template to an express.js API with javascript? I try with the .submit() but it doesnt work. Im using jQueryMObile so the form is inside a data-role page. The route on server: app.put('/users/:userId/spot/:spotId/add', spot.addUserSpot); The form: <form id="follow" method="put"> <input type="hidden" id="spot" value="<%= this.model._id %>"> <input type="hidden" id="user" value="<%= user %>"> <input type="submit" id=

How can one set a sequential delay on adding a class?

安稳与你 提交于 2019-12-12 02:29:21
问题 I'm using jQuery UI plugin and the latest jQuery. I would like to sequentially add on the class, one by one down my array of elements. Right now I have this : $(@el).addClass("gridBoxComplete", 400, "easeOutBounce").delay(800) Where @el is the current element in the array. This, however, does not delay this object before the next item in the iteration is run. I based this animation roughly off of this idea.. $(@).hide().each (index) -> $(@) .delay(index * 100) .fadeIn 500 回答1: delay() delays

Given an array of objects, find the object with a particular key

百般思念 提交于 2019-12-12 02:26:59
问题 Given an array of objects, what is the best way to find the object with a particular key in JS? Using jQuery and underscoreJS is fine. I'm just looking for the easiest / least code answer. Example: An array of objects, where each object has a "name". Find the object with a particular "name". var people = [{name: "A"}, {name: "B"}, {name: "C"}]; My current solution: Pass in the array, the key (e.g. "name"), and the value (e.g. "C"). function getObject(myArray, searchKey, searchValue) { myArray

Sorting nested objects using Javascript and/or Underscore.js

落花浮王杯 提交于 2019-12-12 01:55:15
问题 I'm having trouble sorting an object by TearSheetTypeName and StartDate using Javascript or Underscore.js. The object looks like this: { Components: {141: {TearSheetTypeName: "Skyscraper", StartDate: "2015-01-01"}} {142: {TearSheetTypeName: "Skyscraper", StartDate: "2015-01-01"}} {145: {TearSheetTypeName: "New Car", StartDate: "2015-01-15"}} {146: {TearSheetTypeName: "New Car", StartDate: "2015-01-01"}} } The result I'd like: { Components: {146: {TearSheetTypeName: "New Car", StartDate: "2015

Recreating JavaScript's reduce function

大兔子大兔子 提交于 2019-12-12 01:38:43
问题 I'm trying to recreate the reduce method but as the original function. This helped me to understand a lot but I have one little issue: How to re-create Underscore.js _.reduce method? When I plug in arrays it works right, but objects don't work. I get undefined. I would appreciate advice on what I should change. I commented where I think I should change something but I'm confused exactly what I need to change it to. I also created my own each function. You can find my code here: http://goo.gl

TypeError: invalid 'in' operand n

非 Y 不嫁゛ 提交于 2019-12-12 01:32:43
问题 In the firebug, I have got an error, it told me that it's a type error in the underscore.min.js when I commended out the error line in the month.js, there is nothing wrong I don't know what mistake I have made, I have checked the monthCollection and I'm sure the monthCollection is right I couldn't access the initialize of DayView, the log has no 'success' but just one line 'begin to new a day view' js/views/month.js define([ 'backbone', 'underscore', 'views/day' ], function(Backbone, _,

Backbone.js Collection Not Adding Objects

孤者浪人 提交于 2019-12-12 00:54:24
问题 I am trying to fetch a collection data from the server and load it into my collection object, with Backbone.js. I want to fetch these data at the start, and load my html page with these data. However, the data I downloaded from the server does not get populated into the collection properly. The collection length is zero, do anyone know what I am doing wrong? (function ($) { window.Menu = Backbone.Model.extend({}); window.MenuCollection = Backbone.Collection.extend({ model: window.Menu,

Underscore templates with Backbone view

时光总嘲笑我的痴心妄想 提交于 2019-12-11 23:47:16
问题 I am having trouble passing a Backbone collection to an Underscore template in my HTML. Right now in the template code <%=teamCollection%> is undefined. teamCollection is the Backbone collection I want to pass from the Backbone view into the template. Here is the template code: <script id="user-home-main-table-template" type="text/template"> <table class="table"> <thead> <tr> <th>#</th> <th>Team Name</th> <th>Club</th> <th>Sport</th> <th>Delete?</th> </tr> </thead> <tbody> <% <!--console.log(