request.object.previous() is not working in beforeSave() - parse-platform

I'm trying to check the current and previous value of an attribute in the beforeSave(). So i tried to use request.object.previous("attribute_name") but it is still returning the current changed value. Although the .ditry() is returning TRUE which means that the value is changed. Any idea what is wrong here ? I appreciate your feedback.

I think the .previous() isn't actually part of the Parse.com sdk, but simply inherited from backbone.
In a beforeSave handler, I have something like:
if(object.dirty("attr")) {
console.log("After: " + object.get("attr") + ", Before: " + object.previous("attr")); }
The value returned by 'previous' is always the same. Is this functionality actually
implemented? I've seen a few threads elsewhere that imply it's not -
if so, can you remove it from the API docs until it's done?
If it doesn't work, is the correct workaround to query the previous
object? Or does 'changedAttributes' work?
Oh, I now see that 'previous' is some cruft from Backbone.
source1
previous is a method inherited from Backbone.Model. It won't return the previous value of a field in Cloud Code.
source2
Might not be the answer you're looking for, so as a way to workaround the lack of the .previous implementation this this out:
Don't know if this is helpful or if it would be considered too costly
of a workaround, but you could add a column to the object that is
being updated that stores the previous value of the original column.
This would allow you to access the previous value in the AfterSave
function.

Related

How to update Radis for dynamic values?

I am working with some coaching using Redis in Nodejs.
here is my code implimentation.
redis.get(key)
if(!key) {
redis.set(key, {"SomeValue": "SomeValue", "SomeAnohterValue":"SomeAnohterValue"}
}
return redis.get(key)
Till here everything works well.
But let's assume a situation where I need to get the value from a function call and set it to Redis and then I keep getting the same value from Redis whenever I want, in this case, I don't need to call the function again and again for getting the value.
But for an instance, the values have been changed or some more values have been added to my actual API call, now I need to call that function again to update the values again inside the Redis corresponding to that same key.
But I don't know how can I do this.
Any help would be appreciated.
Thank you in advanced.
First thing is that your initial code has a bug. You should use the set if not exist functionality that redis provides natively instead of doing check and set calls
What you are describing is called cache invalidation and is one of the hardest parts in software development
You need to do a 'notify' in some way when the value changes so that the fetchers know that it is time to grab the most up to date value.
One simple way would be to have a dirty boolean variable that is set to true when the value is updated and when fetching you check that variable. If dirty then get from redis and set to false else return the vue from prior

Get notified when TaggedValue of an element changes in Enterprise Architect

I am new in creating Addins for Enterprise Architect and I have this problem:
I have a diagram with elements which have TaggedValues. I want to get notified when the value of a TaggedValue changes and see the new value.
I saw that there is this event EA_OnElementTagEdit available but I can't seem to get it triggered. I also saw that the tagged value has to be of type AddinBroadcast but I can't seem to make it work. What am I missing?
I will put below a sample of my code:
//creating tagged value
EA.TaggedValue ob3 = (EA.TaggedValue)NewElement.TaggedValues.AddNew("Responsible", "val");
ob3.Value = EEPROMBlocks.ElementAt(index).Responsible;
ob3.SetAttribute("Type", "AddinBroadcast");
ob3.Update();
//event method
public override void EA_OnElementTagEdit(EA.Repository Repository, long ObjectID, ref string TagName, ref string TagValue, ref string TagNotes)
You are not missing anything. This is simply not possible. The only way around is the OnContext... where you temporarily store the status of one element and look if a tag has changed when the context changes. I would not recommend that since it involved a lot of superfluous DB accesses.
Send a feature request (if you are an optimistic guy). Alternatively you should think of ways to get around this somehow else.

select only the most recent value from a Store

some searching on SO leaves me to believe that there is no good (simple) way of achieving the question in the title. The following threads are all very related:
Get current state in ngrx
Getting current state in ngrx
Get current ngrx state without subscribe
Using ngrx to obtain store's current state once
How to get current value of State object with #ngrx/store?
From the last thread ^, it seems that the only way of achieving this is to use withLatestFrom() or combineLatest().
It can't be the case that the following is the only way to make sure you only receive 1 item and that this item is the most recent one:
of('terribleLatestValueHack').pipe(
withLatestFrom(this.store.select(itemSelector))
).subscribe((stringAndItem: [string, Item]) => {
const [, item] = stringAndItem;
// do something with item
});
Given that selecting one most recent item from a Store
is a (very) simple use-case
seems to be a highly requested functionality
I would really like to know why the existing support (apparently there was - in NGRX v2 - according to How to get current value of State object with #ngrx/store?) for it has been removed.
I forgot to add that I was of the impression that store.select(...).pipe(first()) does not return the most recent item from the store. But it seems it does and the cause for any faulty values it returns lies in our own code.
store.select(...).pipe(first()) indeed just does that: return one current item from a Store (and complete the Observable).

Multiple parallel Increments on Parse.Object

Is it acceptable to perform multiple increment operations on different fields of the same object on Parse Server ?
e.g., in Cloud Code :
node.increment('totalExpense', cost);
node.increment('totalLabourCost', cost);
node.increment('totalHours', hours);
return node.save(null,{useMasterKey: true});
seems like mongodb supports it, based on this answer, but does Parse ?
Yes. One thing you can't do is both add and remove something from the same array within the same save. You can only do one of those operations. But, incrementing separate keys shouldn't be a problem. Incrementing a single key multiple times might do something weird but I haven't tried it.
FYI you can also use the .increment method on a key for a shell object. I.e., this works:
var node = new Parse.Object.("Node");
node.id = request.params.nodeId;
node.increment("myKey", value);
return node.save(null, {useMasterKey:true});
Even though we didn't fetch the data, we don't need to know the previous value in order to increment it on the database. Note that you don't have the data so can't access any other necessary data here.

NeDB Update does not work

With NeDB the first statement will update the data correctly, but the second (using the doc value itself as key(and yes docs[i].ID is '2013000060')
won't work - even the result of the update function tells me that 1 row was changed.
1. oDB.update({ MYID: '2013000060' }, { $set: { "PAGE": 2 }}, ...
2. oDB.update({ MYID: docs[i].ID}, {$set: {"PAGE": 2}}, ...
Some ideas?
Take in account that updates in NeDB work asynchronously.
Anything you want to do after updating an object (and relying on that updated values) should be done inside the callback function you pass to the .update() call.
After fiddling around with NeDB datastore.js it turned out that this is sort of a weird timing problem.
The value is actually updated, but when a .find with a query was issued, the value was not yet persisted.
Anyway did I not go deeper to investigate the fact that using string literals yielded different results.
Could you copy paste the exact code you're using, the expected results and the actual results ? From the look of it it seems like you are using synchronous code where you should use callbacks.

Resources