What is expected from a SlickGrid editor factory? - javascript

Should the return value of SomeEditorFactory.getEditor(column) be a function or an object?
From the code
function getEditor(row, cell) {
var column = columns[cell];
var rowMetadata = data.getItemMetadata && data.getItemMetadata(row);
var columnMetadata = rowMetadata && rowMetadata.columns;
if (columnMetadata && columnMetadata[column.id] && columnMetadata[column.id].editor !== undefined) {
return columnMetadata[column.id].editor;
}
if (columnMetadata && columnMetadata[cell] && columnMetadata[cell].editor !== undefined) {
return columnMetadata[cell].editor;
}
return column.editor || (options.editorFactory && options.editorFactory.getEditor(column));
}
Not immediately obvious.

It should be an object that implements getEditor(columnDefinition) and returns the editor for a given column.
See https://github.com/mleibman/SlickGrid/wiki/Writing-custom-cell-editors and https://github.com/mleibman/SlickGrid/blob/master/slick.editors.js.

When SlickGrid calls YourEditorFactory.getEditor(column), it expects a function.

Related

DRY way to throw the same TypeError in more than one functions

I have some functions that each one uses an object as an argument.
All these objects have similar structure, so instead of making the same checks in each function, I've created some check functions:
const checkIfOptionsIsObject = options => {
if (typeof options === 'object') {
return true;
} else {
throw new TypeError('The function argument should be an object.');
}
}
and
const checkOptionsMinMax = options => {
if (isNaN(options.min) || isNaN(options.max)) {
throw new TypeError("'min' and 'max' should both be numbers.");
} else if (options.min > options.max) {
throw new Error("'min' shouldn't be greater than 'max'");
} else {
return true;
}
}
And here is how I am using them:
const firstFunction = options => {
checkIfOptionsIsObject(options);
checkOptionsMinMax(options);
// do some stuff
return result;
}
const secondFunction = options => {
checkIfOptionsIsObject(options);
checkOptionsMinMax(options);
// do some other stuff
return result;
}
const thirdFunction = options => {
checkIfOptionsIsObject(options);
// do some stuff that doesn't use min and max
return result;
}
Is there any problem in this code?
1) note that in your first check typeof object === 'object' if that variable named object is actualy an array it will also give you the type of 'object'. You can see this in your console by entering typeof [ ] === 'object' and notice that it'll return true. It's better to use o !== null && typeof o === 'object' && Array.isArray(o) === false; for object testing.
const checkIfOptionsIsObject = options => {
if (options !== null && typeof options === 'object' && Array.isArray(options) === false) {
return true;
} else {
throw new TypeError('The function argument should be an object.');
}
}

Only include a variable in an if statement, if another variable exists?

The following function is meant to check to see if a custom Date widget (javascript) is empty or not. Problem is, there are many variations of this widget where M/D/Y fields display, or it could be M/D or, M/Y.
Of course, I could hard code all the combinations as if checks, but is there a better way of saying "there are 3 possible nodes, that might have values...if x out of 3 nodes exist AND they all have values, set empty to false."
checkIfEmpty: function () {
var empty = true;
var mNode = this.getNode('month');
var month = mNode ? mNode.value : null;
var dNode = this.getNode('day');
var day = dNode ? dNode.value : null;
var yNode = this.getNode('year');
var year = yNode ? yNode.value : null;
if (month && day && year) {
empty = false;
}
return empty;
}
checkIfEmpty: function () {
var empty = true;
var dateParts = [];
var mNode = this.getNode('month');
if(mNode && mNode.value){
dateParts.push('month');
}
var dNode = this.getNode('day');
if(dNode && dNode.value){
dateParts.push('day');
}
var yNode = this.getNode('year');
if(yNode && yNode.value){
dateParts.push('year');
}
if (dateParts.length) {
empty = false;
}
return empty;
}
You can add to see if the node does not exist
if ( (!mNode || month) && (!dNode || day) && (!yNode || year) ) {
checkIfEmpty: function () {
var empty = false;
var mNode = this.getNode('month');
if(mNode && !mNode.value) {
empty = true;
}
var dNode = this.getNode('day');
if(dNode && !dNode.value) {
empty = true;
}
var yNode = this.getNode('year');
if(yNode && !yNode.value) {
empty = true;
}
return empty;
}
Trying to solve my own question - so far, this is the most efficient way of achieving what I am trying to do. Anyone, suggestions on how to make it even more efficient?
If the value property exists for all valid nodes then:
if(mNode && dNode && yNode){
empty = false;
}
Otherwise:
if(mNode && mNode.value && dNode && dNode.value && yNode && yNode.value){
empty = false;
}
I am not sure if I followed, but if you need that at least one to be true so empty is false then:
if(mNode || dNode || yNode) {
empty = false;
}
Again, if the value property is not standard for all nodes:
if((mNode && mNode.value) || (dNode && dNode.value) || (yNode && yNode.value)){
empty = false;
}
I think it's clearer if you think about it this way:
If (node && node.value) returns a truthy value then the date property exists otherwise the date property doesn't exist.

Game Maker JS Extension

Hi I'm looking to add javascript functions to game maker, but the format of them is like this:
companyname.initialize({
soundMuteCallback: muteSound, // optional
soundUnmuteCallback: unmuteSound // optional
});
And in the file they look like this
this.initialize = function(params) {
companyname.getSharedEventCenter().postEvent(SharedEventKeys.API_INITIALIZE);
_isInitialized = true;
if (typeof params !== "undefined") {
var muteSoundCallback = ("soundMuteCallback" in params && typeof params["soundMuteCallback"] === "function") ? params["soundMuteCallback"] : undefined;
var unmuteSoundCallback = ("soundUnmuteCallback" in params && typeof params["soundUnmuteCallback"] === "function") ? params["soundUnmuteCallback"] : undefined;
_adsManager.setSoundCallbacks(function() {
typeof muteSoundCallback === "function" && muteSoundCallback();
[].forEach.call(document.getElementsByTagName("audio"), function(element){
element.muted = true;
});
}, function() {
typeof unmuteSoundCallback === "function" && unmuteSoundCallback();
[].forEach.call(document.getElementsByTagName("audio"), function(element){
element.muted = false;
});
});
}
_tryShowAd();
};
Does anyone have any idea how to do this in game maker? I don't know what information to put in the extension function properties.
Thanks,
Mitchell.
I would recommend creating a new function that Game Maker can understand and then use that to create your object and the constructor you are showing here.
company.initialize = function(params) {
companyname.getSharedEventCenter().postEvent(SharedEventKeys.API_INITIALIZE);
_isInitialized = true;
if (typeof params !== "undefined") {
var muteSoundCallback = ("soundMuteCallback" in params && typeof params["soundMuteCallback"] === "function") ? params["soundMuteCallback"] : undefined;
var unmuteSoundCallback = ("soundUnmuteCallback" in params && typeof params["soundUnmuteCallback"] === "function") ? params["soundUnmuteCallback"] : undefined;
_adsManager.setSoundCallbacks(function() {
typeof muteSoundCallback === "function" && muteSoundCallback();
[].forEach.call(document.getElementsByTagName("audio"), function(element){
element.muted = true;
});
}, function() {
typeof unmuteSoundCallback === "function" && unmuteSoundCallback();
[].forEach.call(document.getElementsByTagName("audio"), function(element){
element.muted = false;
});
});
}
_tryShowAd();
};
function createMuteCallback() {
muteCallback = function () {
// Code to handle the callback
}
return muteCallback;
}
function createUnmuteCallback() {
unmuteCallback = function () {
// Code to handle the callback
}
return unmuteCallback;
}
function createCompany (mute, unmute) {
if (mute == 1) {
soundMuteCallback.createMuteCallback();
}
if (unmute == 1) {
soundUnmuteCallback.createUnmuteCallback();
}
company.initialize(soundMuteCallback, soundUnmuteCallback);
}
So all of that goes in the same .js file. Create a new extension in Game Maker. Add the .js file to that extension. Add a function named createCompany with two optional parameters.
Then when you call createCompany(1, 1); in your Game Maker code, the .js file will run and will initialize the company object with the two callback functions.
Hope this helps.

JavaScript true false not executing correctly in if else statement

For some reason when I'm using the true/false values and checking if at least one of the values is true, the if/else statement is not working correctly.
I have this:
$scope.checkValues = function (qId) {
var airport = $scope.airports[0].questID;
var destAirport = $scope.destAirports[0].questID;
var airportVal = isFalseOrUndefined($scope.answers[airport]);
var destAirportVal = isFalseOrUndefined($scope.answers[destAirport])
if (airportVal == false || destAirportVal == false) {
$surveyNav.skipPage = true;
}
}
function isFalseOrUndefined(val) {
if(val == null || val === false) {
return true;
} else {
return false;
}
}
In this image below, as you can see the value for airportVal is true, the other value for destAirportVal in that same scenario is true, but I'm still able to get correctly in to the if condition and set the scope value.
Does anyone see any issue?
You should be using === and !== operators when checking for equality in Javascript.
Javascript Comparison and Logical operators
op1 === op2 - Will check if op1 is explicitly equal to op2
op1 !== op2 - Will check if op1 is not explicitly equal to op2
Also: you can condense you isFalseOrUndefined function
Note 1: you are not actually checking if val is undefined.
To check if something is undefined: typeof val === 'undefined'
This is different than checking if a variable is null
Note 2: Keep in mind that your variables are not entirely clear here. airportVal will be equal to true when $scope.answers[airport] is false or null. Is this your intention?
$scope.checkValues = function (qId) {
var airport = $scope.airports[0].questID;
var destAirport = $scope.destAirports[0].questID;
var airportVal = isFalseOrUndefined($scope.answers[airport]);
var destAirportVal = isFalseOrUndefined($scope.answers[destAirport])
if (airportVal === false || destAirportVal === false) {
$surveyNav.skipPage = true;
}
}
function isFalseOrUndefined(val) {
return (val === null || val === false);
}
Your function should probably do what it claims to do:
function isFalseOrUndefined(val) {
return typeof val === 'undefined' || val === false || val === null || val === ''/* add this if you think it should be falsy*/;
}
But then, testing for !val should be sufficient:
$scope.checkValues = function (qId) {
var airport = $scope.airports[0].questID;
var destAirport = $scope.destAirports[0].questID;
if (!airport || !destAirport) {
$surveyNav.skipPage = true;
}
}

javascript data getting undefined

Variable CIMtrek_Attachement_1 getting undefined it is not checking the if condition it moving to this line var selecteditems = CIMtrek_Attachement_1.split("\\");
var CIMtrek_Attachement_1= $("#CIMtrek_RegWhseCo_Attachement_1").val();
alert("CIMtrek_Attachement_1=666666==>> "+CIMtrek_Attachement_1);
if (CIMtrek_Attachement_1 !== null && CIMtrek_Attachement_1 !== "" && CIMtrek_Attachement_1 === undefined)
{
var selecteditems = CIMtrek_Attachement_1.split("\\");
var filename = selecteditems[(selecteditems.length-1)];
alert("selecteditems=666==>> "+selecteditems);
alert("filename=66666==>> "+filename);
nodeField = createNodeField(xmlDoc, new Array("FieldName"), "Record")
nodeField.attributes[0].value = NewFormFields[i]
nodeField.appendChild(xmlDoc.createTextNode(filename));
rootElement.appendChild(nodeField);
}
try typeof(CIMtrek_Attachement_1) === undefined :) and shouldn't it be !== instead of === ?

Categories