NanoStore
NanoStore copied to clipboard
Add a changed? method
I want to know if any fields or even a particular field has changed but not saved yet. Useful for forms and validation.
This seems a duplicate of https://github.com/tciuro/NanoStore/issues/100.
Keeping track of the state at the individual level would be quite a bit of work with the current implementation. The global flag (i.e. hasUnsavedChanges ) is definitely easier.
Understood on the individual attribute level. But changed? and saved? are different. model = Model.find(:id: 1) model.saved?.should == true model.changed?.should == false
model.foo = "bar"
model.saved?.should == true model.changed?.should == true
model.save model.saved?.should == true mode.changed?.should == false
model = Model.new model.saved.should == false
.saved? is to understand if a model has been written to db or not. .changed? is to understand if a model is different from it's saved state.
Yes, I understand the difference, but one could argue that 'not saved' is equal to 'changed'. But I understand that you would want a finer control over what properties have changed. This would require a bit of work and time that I cannot devote, unfortunately.
The changes to https://github.com/tciuro/NanoStore/issues/100 should be easier, though with one caveat: if you set a property and then set it back to its former value, the object will still be marked as changed. To be more precise, just touching an object will mark it as dirty until you save it. I know it's a limitation, but this is the best I can do with the current architecture.
The main idea when I wrote NanoStore was to blast the entire object over an existing one. This was faster than cherry-picking the changes and replacing them individually. This may not be ideal for changes that need to be propagated over the network, but for a local file system, it's fast and the complexity of the code a bit smaller.
Appreciate the effort. Maybe after you post this change, I'll see if I can jump in make the iOS changes myself? I know from a ruby side I could do this pretty easily. Right now I'm subclassing NanoModel to track field level changes and it's not done yet.
And yes I think what you are proposing makes a lot of sense. Keeping it simple and fast is well worth with.