Really simple method to easily round with javascript:
function round (number, fractionalDigits) {
"use strict";
if (!number) {
return;
}
var multiplicator = 1;
while (fractionalDigits--) {
multiplicator *= 10;
}
return Math.round(number * multiplicator) / multiplicator;
}
Just pass in the number to round and the number of fractionalDigits you'd like to keep, like:
round(95.12345, 2); will return 95.12
round(95.54555, 4); will return 95.55
If you omit the last param, you'll get no fractionalDigit, like:
round(95.12345); will return 95
round(95.50000); will return 96
One last call: Don't use it as is, create a namespace for it to reside in!
Or append it to Number, then you can remove the number param. ;)
Number.prototype.round = function(fractionalDigits) {
"use strict";
var multiplicator = 1;
while (fractionalDigits--) {
multiplicator *= 10;
}
return Math.round(this * multiplicator) / multiplicator;
}
Call it like:
var number = 8.12345;
number.round(2); will return 8.12
Have fun!
Posts mit dem Label javascript werden angezeigt. Alle Posts anzeigen
Posts mit dem Label javascript werden angezeigt. Alle Posts anzeigen
Freitag, 22. Juni 2012
Freitag, 17. Februar 2012
No need for temporary variable (Javascript)
Assume you have a function, and that function has a param (called 'config' herein) which may be a string or an object. Inside your function, you check for the type and if it's a string, you want to make this strings value a property of your object.
My second approach looked like this:
I thought about what's happening in those statements and realized that the temporary variable 'url' is not needed, because javascript is executing the assignement from right to left, means I can override 'config' with an object that keeps the original value of config as a property. See this third & final approach:
So Javascript builds the object, assigns the value of config (string) to videoHash and assigns this object to config, which now is a object as desired.
My first, fast approach was this one:
var url = config;
config = {};
config.videoLink = url;
var url = config;
config = {videoLink: url};
config = {videoHash: config};
So Javascript builds the object, assigns the value of config (string) to videoHash and assigns this object to config, which now is a object as desired.
Mittwoch, 21. Dezember 2011
setTimeout() && setInterval() scope problem solver
This is so handy from time to time:
setInterval(
function () {
var myVar = document.getElementById("givenID");
setTimeout(
function() {
// myVar is available because the inner closure
// gets the outer closures scope
myVar.innerHTML = "Junk";
},2000);
}, 10000);
Source: Stackoverflow
Dienstag, 20. Dezember 2011
Sencha Touch 1.1 - Adding a record and removing it doesn't work
I did the following and it worked well:
Add an entry - reload the app - delete that entry
I did the following and it didn't work:
Add an entry - delete that entry
Why?
I started to debug both cases paralelly. In both cases I found the new record in the object that is used to determine what has to be done, but for some strange reason in the case of adding and directly deleting that entry Sencha Touch doesn't find the entry.
What I did is really simple: After running the store.remove(record) method I check if there's a record in the store.removed property. If not, I add that record manually. You can as well fully remove the store.remove(record) method call and write the record directly into the property. It's up to you.
After you did that you just need to call store.destroy() to remove the record.
After you did that you just need to call store.destroy() to remove the record.
Sencha Touch 1.1 - CRUD actions & callbacks via REST proxy
CRUD actions
Methods without value param (update, destroy):
store.update({
callback: function(answer, object, success) {
if (success) { ... }
}
});
store.destroy({
callback: function(answer, object, success) {
if (success) { ... }
}
});
Methods with value param (create, load):
store.create(values, {
callback: function(answer, object, success) {
if (success) { ... }
}
});
store.load({
callback: function(answer, object, success) {
if (success) { ... }
}
});
CRUD action executes only once?
If you can run some of these functions once and after that no XHR is made, it may be because you encountered this bug: Bug @ Sencha Forum
I solved this bug by setting store.snapshot to false, so the function that engages the XHR is no longer looking for changes in the snapshot but in the store object.
Methods without value param (update, destroy):
store.update({
callback: function(answer, object, success) {
if (success) { ... }
}
});
store.destroy({
callback: function(answer, object, success) {
if (success) { ... }
}
});
Methods with value param (create, load):
store.create(values, {
callback: function(answer, object, success) {
if (success) { ... }
}
});
store.load({
callback: function(answer, object, success) {
if (success) { ... }
}
});
CRUD action executes only once?
If you can run some of these functions once and after that no XHR is made, it may be because you encountered this bug: Bug @ Sencha Forum
I solved this bug by setting store.snapshot to false, so the function that engages the XHR is no longer looking for changes in the snapshot but in the store object.
Mittwoch, 7. Dezember 2011
All Methods from a Javascript Object
I wrote that little helper because webdeveloper toolbar didn't show all methods to me. Maybe it's useful for you too.
https://gist.github.com/1441816
https://gist.github.com/1441816
Donnerstag, 1. Dezember 2011
console.log & debug flag?
Instead of checking the DEBUG flag everytime you need a debug info, like this:
if (DEBUG) {
console.log("my debug output");
}
you can just check the DEBUG flag once and disable the console.log function, like this:
if (DEBUG === false) {
console = {
log: function() {}
};
}
This enables you to simply call:
console.log("my debug output")
in your application. If DEBUG is true, you'll get your output. If DEBUG is false, console.log() will be called but is an empty function and thus will produce no output / error.
if (DEBUG) {
console.log("my debug output");
}
you can just check the DEBUG flag once and disable the console.log function, like this:
if (DEBUG === false) {
console = {
log: function() {}
};
}
This enables you to simply call:
console.log("my debug output")
in your application. If DEBUG is true, you'll get your output. If DEBUG is false, console.log() will be called but is an empty function and thus will produce no output / error.
Samstag, 23. Juli 2011
JS Module Pattern
var someModule = (function(){//private attributes var privateVar = 5; //private methods var privateMethod = function(){ return 'Private Test'; }; return { //public attributes publicVar: 10, //public methods publicMethod: function(){ return ' Followed By Public Test '; }, //let's access the private members getData: function(){ return privateMethod() + this.publicMethod() + privateVar; } } })(); //the parens here cause the anonymous function to execute and return someModule.getData();SOURCE: http://addyosmani.com/resources/essentialjsdesignpatterns/book/
Dienstag, 19. Juli 2011
Nyromodal strips / removes script tag on ajax / xhr load
Got me some time to get 'round this, here's the solution:
"If you need to use an external JavaScript content, you could add the attribute rel="forceLoad" to the script tag and nyroModal will load it during the opening modal."
"If you want to use a script which need the content to be visible (many jQuery UI script for instance) you could add the setting rev="shown" to the inline or external script tag to execute it juste before the endShowContent callback."
Source: http://bit.ly/oJ8AeD
"If you need to use an external JavaScript content, you could add the attribute rel="forceLoad" to the script tag and nyroModal will load it during the opening modal."
"If you want to use a script which need the content to be visible (many jQuery UI script for instance) you could add the setting rev="shown" to the inline or external script tag to execute it juste before the endShowContent callback."
Source: http://bit.ly/oJ8AeD
Montag, 18. Juli 2011
Freitag, 8. Juli 2011
On event.preventDefault() error messages in IE 8 (9? 7? 6?)
If checking for event.preventDefault() you get error messages in IE.
A better way is to check for the existence of event.returnValue (which is IEs preventDefault()) and if it doesn't exist you know that you're not on IE.
if (event.returnValue) {
// this is IE
event.returnValue = false;
} else {
// this is Chrome, Firefox, Safari etc.
event.preventDefault();
}
A better way is to check for the existence of event.returnValue (which is IEs preventDefault()) and if it doesn't exist you know that you're not on IE.
if (event.returnValue) {
// this is IE
event.returnValue = false;
} else {
// this is Chrome, Firefox, Safari etc.
event.preventDefault();
}
Mittwoch, 15. Juni 2011
Javascript OOP Cheatsheet
var MyClass = function() {
// self to get "this" in private / global / anonymous methods
var self = this;
// public var
this.publicVar = 'public';
// private var
var privateVar = 'private';
// public method
this.publicMethod = function() {}
// private method
function privateMethod() {}
// global method
globalMethod = function() {}
}
// self to get "this" in private / global / anonymous methods
var self = this;
// public var
this.publicVar = 'public';
// private var
var privateVar = 'private';
// public method
this.publicMethod = function() {}
// private method
function privateMethod() {}
// global method
globalMethod = function() {}
}
Abonnieren
Posts (Atom)