Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ko.validation.group no work on objects with observable _destroy #567

Closed
demitriusbelai opened this issue Jun 11, 2015 · 8 comments
Closed
Milestone

Comments

@demitriusbelai
Copy link

When create observableArray and its elements have observable _destroy, ko.validation.group don't validate these elements.

Test: http://jsbin.com/mijiqifudi/1/edit?html,js,output

Fix:

--- a/src/api.js                                                                                                                                                                   
+++ b/src/api.js                                                                                                                                                                   
@@ -65,7 +65,7 @@                                                                                                                                                                  

                //get list of values either from array or object but ignore non-objects                                                                                            
                // and destroyed objects                                                                                                                                           
-               if (val && !val._destroy) {                                                                                                                                        
+               if (val && !koUtils.unwrapObservable(val._destroy)) {                                                                                                              
                        if (utils.isArray(val)) {                                                                                                                                  
                                objValues = val;                                                                                                                                   
                        }      
@crissdev
Copy link
Member

@demitriusbelai This is by design and it's less probable it will change. Can you describe your use case?

@demitriusbelai
Copy link
Author

First of all, sorry for my English.

Take the Loading and saving data example from Knockout http://jsfiddle.net/rniemeyer/bGsRH/
Users want to review deleted tasks before save and undo deleted tasks. Change foreach binding to "{data: tasks, includeDestroyed: true}". For highlight delete tasks, add css binding "css: {deleted: _destroy}". To work, _destroy need to be observable and need to be added in all tasks. JSON send to server not change, expect by "_destroy": false in non deleted tasks.

Modified example: http://jsfiddle.net/bGsRH/696/

knockout foreach bind use unwrapObservable to filter destroyed:

// Filter out any entries marked as destroyed
var filteredArray = ko.utils.arrayFilter(unwrappedArray, function(item) {
    return options['includeDestroyed'] || item === undefined || item === null || !ko.utils.unwrapObservable(item['_destroy']);
});

Thanks.

@crissdev
Copy link
Member

@demitriusbelai Thanks for taking the time to put all that information here. My oppionion on this is to create some flag around each item, eg. deleted and update the UI accordingly. Afterwards, when you click on Save you can easily detect the deleted items.

@demitriusbelai
Copy link
Author

@crissdev Its works! I have to add onlyIf to validators for check that flag and create custom JSON serialization.
Live example with knockout-validation: https://jsfiddle.net/bGsRH/698/

Last question: Why unwrap _destroy would be a problem if knockout already does that? Its works for both case: plain and observable _destroy.
Thank you very much.

@crissdev
Copy link
Member

@demitriusbelai Taking _destroy into account can confuse users. For example, if you have a form with an input mapped to a value from a destroyed element (which is not displayed) you won't be able to see the error message. The library can't tell if you display the destroyed items or not.

@demitriusbelai
Copy link
Author

@crissdev I explained poorly. O fix don't change this behavior. When _destroy is observable, nothing is vaildated in actual version (!val._destroy is always false because it is a function). O fix skip validation only val._destroy = true or val._destroy = ko.observable(true). Check where this patch is applied. Sorry, I thought the patch explain itself.

@crissdev crissdev added this to the 2.1.0 milestone Jan 6, 2016
@crissdev
Copy link
Member

crissdev commented Jan 6, 2016

Related PR #596

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@crissdev @demitriusbelai and others