Category: javascript

Three ways to add dropdown options via Javascripts 0

Three ways to add dropdown options via Javascripts

Method #1 - Via jQuery + html tag var self = this; var sel = (self.selected === -1)?true:false; var option = null; option = $(""); option.val(-1).prop("selected", sel).text("(None)"); this.$el.append(option); this.collection.forEach(function(model) { sel = (self.selected ==...

Backbone Collection Flexible Value Sorting Solution 0

Backbone Collection Flexible Value Sorting Solution

Model/Collection Declaration Inputs.Collection = Backbone.Collection.extend({ model: Inputs.Model, url: "/apis/v2/inputs", sortAttribute: "name", // supported attributes are model's element names + any custom strategies below comparator: function(model) { return Utils.genericComparator.call(this, model); }, strategies: {}, changeSort: function...

jQuery prevent uploading too large file in Frontend 0

jQuery prevent uploading too large file in Frontend

afterRender:       // Only works on HTML5       $('#file').on('change', function() {         $("#upload-package").prop("disabled",  false);         var sizeLimit = 90;         if (this.files && this.files.length && this.files[0].size > sizeLimit*1024*1024) {           notifier.error_x("File size exceeds limit...

Mousewheel prevent body scroll on modal 0

Mousewheel prevent body scroll on modal

Solution #1   // prevent background of modal from scrolling   // DOMMouseScroll for Firefox equivalent   $(document).on("mousewheel DOMMouseScroll", ".modal-backdrop, .modal-body", function(event) {     event.stopPropagation();     event.preventDefault();   });   // prevent background of...

Single beforeEach() in Jasmine 0

Single beforeEach() in Jasmine

  << @philipraath @olignyf a beforeAll or beforeEach that is not inside of any describe will be applied to all suites/specs, as appropriate, during the execution of the suite. >> https://github.com/jasmine/jasmine/issues/811

Upgrade Jasmine 1.3 to 2.4 0

Upgrade Jasmine 1.3 to 2.4

Quick upgrade with those Regexp replaces /createSpyObj/ => " jasmine.createSpyObj" /createSpy/ => "jasmine.createSpy" /\.andCallFake/ => ".and.callFake" /\.andReturn/ => ".and.returnValue" /\.andCallThrough/ => ".and.callThrough" /toNotMatch/ => "not.toMatch" /this.addMatchers/ => "jasmine.addMatchers" /\.calls\[0\]\./ => ".calls.first()" /\.calls\[(\d)\]\./ => ".calls.all()[$1]"...

String manipulations in Javascripts 0

String manipulations in Javascripts

String.indexOf() Return value: -1 if not found 0 or higher than 0 as the index of the position found Remove one part of a string Method 1: String.replace(//, ""); Method 2: var indexEnd =...

Submit a form with ajax 0

Submit a form with ajax

This is a basic operation that you might have to do when upgrading an old web app. One solution is using jQuery .serialize() function   Method #1: Sent using CGI params key=value pairs: var...