I have a store with items retrieved from server. I would like to detect changes to alter UI accordingly.
I am able to detect dirty records (new, updated), but I don't know how to detect deletion, which I also need to handle...
An illustration of my problem: http://jsfiddle.net/MikeAski/bBUB2/
Any idea?
There are two things you need to do:
Keep track of what posts were originally there so you can mark it as dirty if one is deleted.
Keep track of new posts, and if they are deleted then mark that part as clean.
Because of your call to each post's isDirty function, you are only checking if the current posts have been updated or created. You basically need a snapshot of what posts exist when it is clean, and then you can compare if any of those have been deleted.
You can also keep a record of newly added posts. That way when one is deleted you can check if it was added(add a isNew flag or something similar). Then when it is deleted you can check if it isNew and mark it clean again otherwise it makes it a dirty delete.
To do these things, you will need a function that checks for deletes as the first dirty check and then check the posts like you currently are.
I finally found a way out: http://jsfiddle.net/MikeAski/bBUB2/7/
This solution is less intrusive on models than the one suggested by jsworkman.
Not fully satisfying, but works as expected... :-/
Still interested in better implementation rather than workaround-flavoured solution!
Related
I tried django-simple-history and I discovered that every create or update is stored twice in the history model. I don't know which information is now useful for you to help me, but I use CBV's and model forms. I followed the instructions on how to install and setup and everything works fine. What I'm wondering is why there is a command line called clean_duplicate_history, which indeed removes all duplicates records. Thank you in advance for any help.
django-simple-history is naive. It works by creating a new simple history record on a post_save signal. Thus, it creates a new record every time you .save regardless of whether anything has changed. Because of that, duplicate records may increase significantly, which is why there is a clean_duplicate_history utility method. If the same record is being store twice and you're unsure why, it's likely that you're making multiple saves.
I'd like to augment events/occurrences in django-scheduler with three things:
Location
Invitees
RSVPs
For Location, my initial thought was to subclass Event and add Location as a foreign key to a Location class, but my assumption is that each occurrence saved won't then include Location, so if the location changes for one occurrence, I'll have nowhere to store that information.
In this situation, is it recommended to create an EventRelation instead? Will I then be able to specify a different Location for one occurrence in a series? The EventRelation solution seems untidy to me, I'd prefer to keep models in classes for clarity and simplicity.
I think Invitees is the same problem, so presumably I should use a similar solution?
For RSVPs, I intend to make an RSVP class with Occurrence as a foreign key, and as far as I can tell that should work without any issues as long as I save the occurrence before attaching it to an RSVP?
I've read all the docs, all the GitHub issues, various StackOverflow threads, the tests, the model source, etc, but it's still unclear what the "right" way to do it is.
I found a PR which introduces abstract models: https://github.com/llazzaro/django-scheduler/pull/389 which looks like exactly what I want, but I'm reluctant to use code which was seemingly abandoned 18 months ago as I won't get the benefit of future improvements.
EDIT: I'm now thinking that another way to do this would be to have just one object linked to the event using EventRelation, so I'd have an "EventDetails" object connected to the Event via EventRelation, then include FKs to Location, Guests, etc from that object.
I should then also be able to subclass my EventDetails object with different kinds of events and attach those too. I'll give it a go ant see if it works!
Just in case anyone find this and is wondering the same thing: I ended up ditching Django-scheduler and using Django-recurrence instead. Had to do a bit more work myself, but it was easier to create the custom event types that I was looking for. Worked pretty well!
I have a QTreeWidget where upon clicking the expand of an item - when the itemExpanded signal is emitted, I use an algorithm to construct a QList<QTreeWidgetItem*> and add it to the expanded item with QTreeWidgetItem::addChildren.
This works, however if I collapse and expand it again, it adds more children, so I get duplicates. So I have to remove the old children first. For that I use qDeleteAll(item->takeChildren()) and then I add the new ones with addChildren. The thing is, it doesn't work for some reason. It remains empty.
Is there some standart way of performing such an operation?
It sounds like you really want to use a QTreeView with a custom QAbstractItemModel. Yes, that may look a bit scary, if you have never done this before, but the key advantage is that in this approach, the view will ask you for item data, if and when it is needed, without you having to pre-fill it.
Alternatively, do consider the advice given by Michael Boone: Instead of deleting all child items, then re-adding them, why don't you just check whether child items have already been added (optionally updating them, if required)?
I had problem with saving relation to object with same class as parent.
You can check this problem here.
When I read that I can easily set the relationship after the promise has fulfilled here I created another example with that info in mind. But it doesn't work as I expect.
What I expect
Create array of Box instances with relation to previous Box instance in each.
And the question is if I'm doing something wrong or it's a bug. Let me know if you need any informations.
Your example isn't clear and simple enough. It needs to be isolated to EXACTLY what you're having an issue about and nothing else.
Having said that, I have had quite a bit of success saving relations to objects with the same class as parent, and so I don't think this is a problem with Ember Data or Ember.
Your code is quite convoluted and uses the sync library, which I'm not faimilar with.
It's a good idea to things as simply as possible at first, so try creating a jsbin with just the isolated functionality relating to saving relations that you're attempting, and then adding additional layers of functionality and testing after each add.
I'm confused about the correct way to reset or clear the data associated with a QAbstractItemModel.
I'm writing an application in which the user can "start over" with a new set of data (empty, or small).
Should I be deleting the old model when the user makes this request? Or should I leave the model alone and just remove all of the rows?
Regards,
Dan O
Generally I would prefer to have the model react to changes and take the necessary actions to update it's view (indirectly ofcourse). However, programming models can be (=is) a PITA, so I would probably look through the fingers if I was reviewing code that created a new model and deleted the old one. Only do this if you are sure the user only will delete all rows. If the user may delete items from the model incrementally you're probably best off implementing removal properly in the first place...
Also, ModelTest might help you discover problems with your Qt models.
If the user is truly starting over with a new set of data, then it makes sense to me to simply delete the old model and create a new one. Simple, effective, and it matches up to what the user is doing.
I don't know which way it truly "better" but removing all the rows can be a rather simple function something like:
void MyModel::Clear(void)
{
// remove all data from internal data structures
...
// Call QAbstractItemModel::reset to ensure any views know that everything has changed.
reset();
}