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

resetting/clearing a model: Uncaught TypeError: cb is not a function #644

Closed
mkastner opened this issue Jul 22, 2016 · 9 comments
Closed

Comments

@mkastner
Copy link

Maybe I am missing something fundamental.

I am trying to clear out error messages from a form, by replacing the the error model with an empty Object.

My model structure looks like this, when the validation fails:

models: {
    error: {
        errors: {
            email: {
                message: 'not a valid address',
            },
            password: {
                message: 'is required'
            }
        }
    }
    login: {
        email: '',
        password: ''
    }
}

In order to get rid of the error messages after the validation the error object is set to empty {}

models.error = {};

But when that happens, this error is being raised:

Uncaught TypeError: cb is not a function                        rivets.js:1532

Obviously the program expects a function at this point. What's really there is an undefined value.

@mkastner
Copy link
Author

I think, it was connected to this:

#611

@jccazeaux
Copy link
Contributor

I just had the same problem. I think it's due to this commit
The code

 callback() for callback in callbacks[keypath].slice() when callback in callbacks[keypath]

has been replaced with

cb() for cb in callbacks[keypath]

Some code is missing and causes the error.
@mkastner Could you please reopen the issue
@blikblum Do you remember why you didn't code the callbacks[keypath].slice() when callback in callbacks[keypath] ?

@blikblum
Copy link
Contributor

blikblum commented Aug 5, 2016

The only requirement to avoid the mem leak is rename the callback var from callback to cb

The other changes were to remove redundant code:
when callback in callbacks[keypath] is redundant and slice is just duplicating the array

Anyway, the fastest way to discover is replace with the old code.

try
cb() for cb in callbacks[keypath].slice() when cb in callbacks[keypath]

As a side note a jsfiddle showing the error and tests are desirable

@jccazeaux
Copy link
Contributor

I don't quite understand this part of code, but it seems necessary to copy the array (with slice()) in order to preserve the original size. In some situations the array (out of scope) may change during process.
Here is a fiddle to show the error
The same fiddle with rivets 0.8.1

This situation is somehow quite similar to #650 and I'm starting to think it hides a vicious bug in unbind process... but it's just a thought

@blikblum
Copy link
Contributor

blikblum commented Aug 6, 2016

I don't quite understand this part of code, but it seems necessary to copy the array (with slice()) in order to preserve the original size. In some situations the array (out of scope) may change during process.

I think you are right.

I suggest changing to

cb() for cb in callbacks[keypath].slice() when cb in callbacks[keypath]

Will fix the mem leak and account for this case.
A test case to avoid future regressions would be good also

@blikblum
Copy link
Contributor

blikblum commented Aug 6, 2016

I reduced the minimum to reproduce the issue: https://jsfiddle.net/3Lbjxbxh/4/

@blikblum
Copy link
Contributor

blikblum commented Aug 6, 2016

I've added a test. You can see here: blikblum@e80e45b

@mkastner mkastner reopened this Aug 7, 2016
@jccazeaux
Copy link
Contributor

Thanks, it's on the PR.

@benadamstyles
Copy link
Collaborator

PR has been merged.

jccazeaux pushed a commit to jccazeaux/rivets that referenced this issue Oct 10, 2016
…ix seems to be already effective on ES6 branch
benadamstyles added a commit that referenced this issue Oct 11, 2016
Report fix for #644 on ES6. Only the test has been reported, fix seem…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants