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.
Related
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.
i have an object stored in localStorage lets say myObject, when ever i get this object then update a key inside of it then try to use localStorage.setItem to update it, it updated all the keys inside that object.
the object keys are unique since they are reflecting other object IDs.
example code:
let obj = localStorage.getItem('myObject') || '{}'
obj = JSON.parse(obj)
obj[this.currentItem.id] = 'some value here'
localStorage.setItem('myObject', JSON.stringify(obj)) // updates all keys inside the object to be the same value
ok i think i knew where the problem for this was but i don't know why it was happening, i am using Vue for my app and in the mounted function i was getting the item details using the id passed in the route, when i tried to console.log(this.item) it was fine until i redirected to another item then it started to log all the items i have already been through in this page before, i have no idea why this is happening, this triggered the localStorage.setItem multiple times with different IDs but the same value i had which ended up updating all the keys in object inside the localStorage as well.
i ended up using the id from the route this.$route.params.id instead and that solved it but i still have no idea why the mounted was triggering multiple times like this and why it was triggering with different object everytime :/
if this is something in Vue about rendering and lifecycle and someone knows why, could you please link some articles so i can read about it, i am still starting in Vue.
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;
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.
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?