javascript variable variable names and global scope (pinesnotify) - javascript

I'm using pines notify (somewhat irrelevant to my issue, I imagine) to create notifications on the change event of a drop down list. On the server side, the change is saved to a database, then when the save is complete - I wish to remove the notification (or do something else depending on the result).
The problem is in creating the notification object in a way that I can later reference it (to remove it).
My intended solution would obtain the id of the dropdownlist, prepend 'pn' to it and use that as the variable name, much like
var pnid = 'pn' + $('#mydropdown').attr('id');
notifications[pnid] = createNotification();
In the codebehind I can create javascript code knowing what the notification object will be called. However I'm struggling with my 'notifications' object.. I've tried this[notifications], window[notifications] etc. to no avail (ie I cannot later reference this object to interact with it) . I'm creating that object outside of any functions like so
var notifications = {};
Am I going about this the completely wrong way?

You can use this line before reaching notifications object.
window.notifications = window.notifications || {};
This will help you create the object if it is undefined and it will also prevent you from overriding it if it already exists.
Note : I assume you have to use this object as a global variable.

Related

Why is my console.log in LWC showing variable data in proxy handler

I'm trying to console.log a variable's value but on the browser console instead of printing the variable (an object in my case), I am getting a Proxy container with format like
Proxy {}[[Handler]]: En[[Target]]: Array(0)[[IsRevoked]]: false
On opening the [[Handler]], I get some inner properties which contains an originalTarget property.
On expanding the originalTarget , my data is shown.
How do I get this data to show properly in console and also access it in my LWC ?
this.variableName returns value in a Proxy
If you want to view proxy data so use this :
JSON.stringify(this.caseList)
And further if you want to use it in your lwc use this:
let cases = JSON.parse(JSON.stringify(this.caseList))
console.log(cases);
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Reason : whenever we mark any Javascript object as #track, Salesforce wraps the object inside creating proxy objects.
Solution: #Rajat Jaiswal answer.

Preventing a section of html from referring to other tags / elements on the same page

I have a php webpage that includes some graphical dials created with css and javascript (and an ajax call). Each dial is added to the webpage using:
$_SESSION['info'] = dial1 Specific info.
include 'dial.php';
$_SESSION['info'] = dial2 Specific info.
include 'dial.php';
Inside dial.php, there is a section that analyzes the SESSION variable to adjust an arm on the dial, and creates the dials circular shape with css. The problem I'm trying to solve is the second dial is a copy of the first dial, and not distinct.
How can I make the above code force each "include 'dial.php'" to operate independently from each other and not interact with each other (since the variables, function names, and css names are the same for each dial).
Best Regards
you can not add two different object into one variable !
make your session variable as an array like below :
$_SESSION['info'][1] = dial1 Specific info.
include 'dial.php';
$_SESSION['info'][2] = dial2 Specific info.
include 'dial.php';
for a better answer , put your dial.php code , I'll update my answer
The session variable can hold many types of data but you need to structure the session variable in a different manner to your initial code. If you were to assign an object as the value ( as below ) you can access the right piece of information easily using the object notation shown below.
$_SESSION['info']=(object)array(
'dial_1' => 'dial1 Specific info',
'dial_2' => 'dial2 Specific info'
);
then access the individual info by
$info=$_SESSION['info']->dial_1;
So I ended up using dynamic html tag ids, function names, and variables. It seems to be working out fine.

Angular refresh scope from service with initial data

I have a service returning static JSON data saving to a $scope variable like this:
$scope.workout = Workouts.get(id);
I´m manipulation this $scope.workout with shift() and slice(). When I try to set $scope.workout to the initial data from the Service, this does not work.
$scope.workout = Workouts.get(id);
The $scope.workout has been changed. I thought that another call of the service would refresh the data? Am I wrong? How to get the initial data saved in the service?
If you're using this for an edit feature, make a copy of the object to make edits to so it doesn't affect the original.
$scope.workoutTarget = Workouts.get(id);
$scope.workout = angular.copy($scope.workout);
//you're free to make edits to $scope.workout without affecting the service
In your save function, commit the changes by merging the objects.
angular.merge($scope.workoutTarget, $scope.workout)
Now $scope.workoutTarget will have the edits applied to it.
1 workaround can be, copy your $scope.workout to another Object and assign it back to $scope.workout.
$scope.workout = Workouts.get(id);
var copyObj = angular.copy($scope.workout);
The object is a reference type. So why you return it from the service, it returns it's reference. It means that you have 2 reference to the same object (from the service and in the scope). So if you change the object from one reference, it will be changed for another reference too.
You need to copy your object.
Simply you can do it by
angular.copy(Workouts.get(id), $scope.workout);
And work with your $scope.workout in your controller.
While I am not sure where your code is wrong (I am not that great at AngularJS!) A quick hack you can use is this:
Assign the initial data to to a new scope variable THEN assign your workout variable from our new scope variable. Now we can refresh workout from our new variable at any time.
You could try $route.reload to refresh the injected data.

How to tell the containing object in javascript?

I have JSON data and I was wondering if there was a way to determine the parent object of a nested object. For example, take this structure:
Vehicles[]
ForSaleCars[]
Car{}
Make
Model
Year
SoldCars[]
Car{}
Make
Model
Year
Assume that this is in var json = ; and has been filled with some data. It is easy to access the second car in the ForSaleCars array like this:
var secondCar = json.Vehicles.ForSaleCars[1];
At this point, solely from the secondCar variable, is it possible to tell it came from ForSaleCars and not SoldCars?
The reason I ask is that I am traversing a json object graph recursively and it would be nice to see which parent the object had without tracking.
At this point, solely from the secondCar variable, is it possible to tell it came from ForSaleCars and not SoldCars?
No. JavaScript doesn't automatically track where a reference was copied from.
It is not possible. You can maintain hash instead.
No, you are copying from the parent, and JavaScript won't track what the parent reference used to be. If you want to be able to track it, you will need to add a reference:
var secondCar = json.Vehicles.ForSaleCars[1];
secondCar.parent = json.Vehicles;

How to dynamically create / destroy JS variables and append / remove their values in a string

I want to create javascript variables on the fly and append their values into a string. I actually am unable to find a method which dynamically declares variables in javascript. Once that is done I would want to do the reverse of this. So my question has two parts : one is related to creating and deleting variables on the fly as per requirement and the other is related to appending or deleting those variables value into a string at a specific point in the string.
There is the eval function.
But! you should ask yourself twice why do you need to "create variables on the fly"
eval on MDN
If I understand what you're after correctly, you should use an object for this:
var o = {};
o.name = "value"; // Creating a property
delete o.name; // Deleting a property
It sounds like you have a specific problem, could you perhaps share a bit more information about your problem?

Categories

Resources