How to initialiaze an object and assign different variables javascript - javascript

I have created an object where i need to assign some variables(parameters) and when the object is called, the variables(parameters) change. Here is my code:
var Modal = {
init: function () {
contact1: "";
contact2: "";
aboutus1: "";
aboutus2: "";
privacy1: "";
privacy2: "";
terms1: "";
terms2: "";
$(".modaltrigger").removeAttr("target");
$(".modaltrigger").click(function () {
if ($(this).is("#contact")) {
$('#primary_url').attr('href', contact1);
$('#secondary_url').attr('href', contact2);
} else if ($(this).is("#aboutus")) {
$('#primary_url').attr('href', aboutus1);
$('#secondary_url').attr('href', aboutus2);
} else if ($(this).is("#termsconditions")) {
$('#primary_url').attr('href', terms1);
$('#secondary_url').attr('href', terms2);
} else if ($(this).is("#privacy")) {
$('#primary_url').attr('href', privacy1);
$('#secondary_url').attr('href', privacy2);
}
});
}
};
I am trying to initialize the object above, and it does not work:
Modal.init(
contact1: "http:www.test1.com";
contact2: "http:www.test2.com";
aboutus1: "http:www.test3.com";
aboutus2: "http:www.test4.com";
privacy1: "http:www.test5.com";
privacy2: "http:www.test6.com";
terms1: "http:www.test7.com";
terms2: "http:www.test8.com"
);

it is Done like this way,
i Guess this is what you want to do.
var Modal = {
init: function (args) {
//then access your values like this
contact1= args.contact1;
contact2 = args.contact2;
..........
.........
.........
}
}
And to initiate this method you have write as
Modal.init({
contact1:"contact str",
contact2:"contact str",
.....
.....
lastitem : "last str"
});

Related

How to add a task in javascript function?

I have a HTML/JS code as shown below in which on click of Execute button, I want to display:
[{"detail":"Hello World"},{"detail":"Great News"}]
Currently, on clicking Execute button I am getting the following:
[{"detail":""},{"detail":""}]
I am wondering what changes I need to make in the JS code below so that on click of a Execute button, I am able to display:
[{"detail":"Hello World"},{"detail":"Great News"}]
HTML:
<input type="submit" onClick="runCode()" value="Execute" >
<div id="console-log">
</div>
Javascript:
$(document).ready(function() {
})
function runCode(){
var td=new Todo()
td.addTask("Hello World")
td.addTask("Great News")
td.printList()
}
class Todo{
constructor(name) {
this.todolist = [];
this.task={
'detail':''
}
}
addToList(newobj)
{
this.todolist.push(newobj)
}
addTask(taskDetail){
this.task.detail=taskDetail
this.todolist.push(this.task)
this.task.detail='' //clear data for next step
}
printList()
{
var consoleLine = "<p class=\"console-line\"></p>";
var text= JSON.stringify(this.todolist)
$("#console-log").append($(consoleLine).html(text));
//alert(this.todolist)
}
}
Push an object created just from the argument into the todolist property:
addTask(detail) {
this.todolist.push({ detail });
}
The this.task property only makes things more confusing, I'd recommend avoiding it.
function runCode() {
var td = new Todo()
td.addTask("Hello World")
td.addTask("Great News")
td.printList()
}
class Todo {
constructor(name) {
this.todolist = [];
}
addTask(detail) {
this.todolist.push({ detail });
}
printList() {
var consoleLine = "<p class=\"console-line\"></p>";
var text = JSON.stringify(this.todolist)
$("#console-log").append($(consoleLine).html(text))
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="submit" onClick="runCode()" value="Execute">
<div id="console-log">
</div>
It would also be trivial to remove the dependency on the big jQuery library if you wanted, it's not accomplishing anything useful. Also, it'd be good practice to use addEventListener instead of an inline handler:
document.querySelector('input').addEventListener('click', () => {
var td = new Todo()
td.addTask("Hello World")
td.addTask("Great News")
td.printList()
});
class Todo {
constructor(name) {
this.todolist = [];
}
addTask(detail) {
this.todolist.push({ detail });
}
printList() {
document.querySelector('code').appendChild(document.createElement('p'))
.textContent = JSON.stringify(this.todolist);
}
}
<input type="submit" value="Execute">
<code>
</code>

Dymanic table names in codemirror

Requirement:
Need to pass tablename and columnname to the sql hint using code mirror at runtime.
Problem Faced:
Column names are passed dynamically ,where table name doesn't.
Code:
function initAutoComplete(tablename) {
alert(tablename);
var tmptables= {
tabledata:["a","b"]
}
CodeMirror.commands.autocomplete = function (cmeditor) {
CodeMirror.showHint(cmeditor,'', {
completeSingle: false,
tables: tmptables
});
}
}
Expected is systemset.columnname
var content={"AAAAAAAA": ["test"]};
function initAutoComplete() {
CodeMirror.commands.autocomplete = function (cmeditor) {
CodeMirror.showHint(cmeditor,'', {
completeSingle: false,
tables: getcontent()
});
}
}
function getcontent(){
return content;
}

Unable to get property 'new_textholder' of undefined or null reference

I'm using code I found online to convert a standard option set field to a checkbox in MS CRM 2015 online. It keeps giving me the above error.
Here is the code:
function MultiPickList3(param1, param2, param3,param4,param5)
{
try
{
var fn = arguments.callee.toString().match(/function\s+([^\s\(]+)/);
if (param1==null || param2==null)
{
alert("Error: Parameter missing. \n Format: <optionset fieldname> , <option value text field> , [<option header>], [<select color>] , [<deselect color>] , \n ["+"Function="+fn[1]+"]" );
return true;
}
var tparamtype=Xrm.Page.data.entity.attributes.get(param1).getAttributeType();
if (tparamtype!="optionset")
{ alert (param1+"(first parameter) should be of type optionset \n"+"[function="+fn[1]+"]");
return true;
}
var tparamtype=Xrm.Page.data.entity.attributes.get(param2).getAttributeType();
if (tparamtype!="memo")
{ alert (param2+"(second parameter) should be of type memo (text with mutiline) \n[function="+fn[1]+"]");
return true;
}
var plOptions=param1;
var plText=param2;
var plMenuItem="View Selected List";
var SelectedList_orig = crmForm.all[plText];
var FullList=crmForm.all[plOptions];
var SelCtr=-1;
var new_selColor="orange";
var new_deSelColor="white";
if (param4!=null)
new_selColor=param4;
if (param5!=null)
new_deSelColor=param5;
var SelectedList =SelectedList_orig.value.split("\r\n");
crmForm.all[plText].style.display="none";
if(FullList!=null && SelectedList!=null)
{
initColor();
if (param3!=null && param3!="")
{
plMenuItem=param3;
}
else
{
plMenuItem=FullList.options[0].text;
changeColor("grey",0);
}
for (var ctr=0; ctr<SelectedList.length;ctr++)
{
selCtr=SelectedIndex(SelectedList[ctr]);
if (selCtr >-1)
{
changeColor(new_selColor,selCtr);
}
}
}
function SelectedIndex(selectedText)
{
var FullListText;
for (var ctr1=0; ctr1<FullList.options.length;ctr1++)
{
FullListText=FullList.options[ctr1].text;
if ( FullListText==selectedText)
{
return ctr1;
}
}
return -2;
}
crmForm.all[plOptions].attachEvent('onchange', OnChangePL);
function OnChangePL()
{
var SelCtr=-1;
var sel=crmForm.all[plOptions].SelectedText;
if (sel==plMenuItem)
return;
SelCtr=SelectedIndex(sel);
var SelColor="grey";
SelColor=crmForm.all[plOptions][SelCtr].style.backgroundColor;
if (SelColor==new_selColor)
{
changeColor(new_deSelColor,SelCtr);
saveChanges(sel,selCtr,"del");
}
else
{
changeColor(new_selColor, SelCtr);
saveChanges(sel,selCtr,"add");
}
}
function saveChanges(p_selText,p_SelCtr,p_mode)
{
switch(p_mode)
{
case "add":
SelectedList.push(p_selText);
break;
case "del":
for (var ctr2=0;ctr2<SelectedList.length;ctr2++)
{
if (SelectedList[ctr2]==p_selText)
{
SelectedList.splice(ctr2,1);
break;
}
}
break;
}
Xrm.Page.getAttribute(plText).setValue(SelectedList.join("\r\n"));
}
function initColor()
{
for (var ctr3=0; ctr3<FullList.options.length;ctr3++)
{ changeColor(new_deSelColor, ctr3); }
}
function changeColor(newColor, newCtr)
{
crmForm.all[plOptions][newCtr].style.backgroundColor=newColor;
}
}
catch (e)
{ alert (e.description);}
}
param2 takes the field "new_textholder" which is a multiline text field. It's defined on the form and I have checked to make sure I am writing it correctly.
What could be the problem?
Thank you!!
This error is likely coming from the crmForm.all[plText] line. I'm guessing your org is recent enough that support for the old 4.0 api is no longer present, which would mean that the all object is no longer available, which would give you that error (plText is set to the value of param2). You might be able to modify this code to work on your 2015 form, but I don't have a 2015 org available to me at the moment so I can't say for sure.

Javascript push Array items in Object

I have the following issue for creating a object in Javascript
When a user clicks a button I want to push the sn_slab to the array slabs. But each serial_number is grouped by the batch_number.
The object should look something like this
var Object = {
'COFBP21018': {
slabs: {
0: 18765,
1: 38947,
...
}
},
'DEPOUS394O': {
slabs: {
0: 11006276,
1: 11020446,
...
}
},
..
}
my html looks like this
<a href=".." class="add_slab_to_array" data-batch_nr="COFBP21018" data-sn_slab="18765" />
<a href=".." class="add_slab_to_array" data-batch_nr="COFBP21018" data-sn_slab="38947" />
<a href=".." class="add_slab_to_array" data-batch_nr="DEPOUS394O" data-sn_slab="11006276" />
<a href=".." class="add_slab_to_array" data-batch_nr="DEPOUS394O" data-sn_slab="11020446" />
var block = {};
$('.add_slab_to_array').click(function(event) {
event.preventDefault();
var batch_nr = $( this ).attr('data-batch_nr');
var sn_slab = $( this ).attr('data-sn_slab');
// Create new object if batch_nr does not exists
// ADD sn_slab to Array inside the Object with related batch_nr
block[batch_nr] = {
slabs: [],
add: function(sn_slab) {
this.slabs.push(sn_slab)
}
}
block[batch_nr].add(sn_slab);
});
The code above works, but my array slabs is always overridden.
It seems to me you're redefining the block object each time you click. You should instead check if it's set before proceeding.
block[batch_nr] = block[batch_nr] || {};
block[batch_nr].slabs = block[batch_nr].slabs || [];
block[batch_nr].add = block[batch_nr].add || function(sn_slab) {
this.slabs.push(sn_slab);
};
Consider this
block[batch_nr] = {
slabs: [],
add: function(sn_slab) {
if (block[batch_nr]) {
block[batch_nr].slabs.push(sn_slab)
}
}
}

Getting ng-show value as reference

I have the following html:
<div ng-show=showMarketingNav>
...
</div>
<div ng-show=showProductsNav>
...
</div>
<div ng-show=showSettingsNav>
...
</div>
What I want to do is to easily be able to hide all but one of the divs from another controller. I thought I could be clever and do the following:
var subNavMenuDisplays = {
Marketing: $scope.showMarketingNav,
Products: $scope.showProductsNav,
Settings: $scope.showSettingsNav
}
$rootScope.hideContextMenu = function () {
for (var category in subNavMenuDisplays) {
subNavMenuDisplays[category] = false;
}
}
$rootScope.setContextMenu = function (name) {
$rootScope.hideContextMenu();
subNavMenuDisplays[name] = true;
}
but this obviously does not work as $scope.showMarketingNav etc. will be passed as value, not reference.
The following works, but is not exactly nice to work with:
$rootScope.hideContextMenu = function () {
$scope.showMarketingNav = false;
$scope.showProductsNav = false;
$scope.showSettingsNav = false;
}
$rootScope.setContextMenu = function (name) {
$rootScope.hideContextMenu();
if (name == "Marketing") {
$scope.showMarketingNav = true;
}
if (name == "Products") {
$scope.showProductsNav = true;
}
if (name == "Settings") {
$scope.showSettingsNav = true;
}
}
Is there a way to grab $scope.showMarketingNav by reference, or another clever way around this?
I'd prefer not using eval to concatenate variable names.
You can place an object on the $scope and then toggle it dynamically:
$scope.show = {};
$rootScope.setContextMenu = function (name) {
$scope.show = {};
$scope.show[name] = true;
}
And the Html:
<div ng-show="show.Marketing">
...
</div>
<div ng-show="show.Products">
...
</div>
<div ng-show="show.Settings">
...
</div>
Here's a plunker demonstrating the change.
You can assign simple updater functions in that object:
Marketing: function(val) { $scope.showMarketingNav = val },
Products: function(val) { $scope.showProductsNav = val},
Settings: function(val) { $scope.showSettingsNav = val}
Then call it:
subNavMenuDisplays[name](true);

Categories