0
validations = []

isEmpty = (string) ->
    string is '' or string is undefined or string == null

createValidation = (scopeVariable, expected, responseText, inverse) ->
    if inverse == undefined
        inverse = false

    if !inverse
        returningValidation = ->
            if scopeVariable isnt expected
                $scope.response.text = responseText
                $scope.response.class = 'text-danger'
                return false
            true
    else
        returningValidation = ->
            if scopeVariable is expected
                $scope.response.text = responseText
                $scope.response.class = 'text-danger'
                return false
            true

    returningValidation

validateCredentials = ->
    validated = true
    validations.map (validate) ->
        if !validate()
            validated = false
    validated

$scope.register = ->
    if validateCredentials()
        #Account.register $scope.form, (response) ->
            #if response.user_created is true
        $scope.response.text = '...'
        $scope.response.class = 'text-success'

validations.push createValidation $scope.form.termsChecked, true, '...'
validations.push createValidation $scope.form.password, $scope.form.passwordRepeat, '...'

inverse = true
validations.push createValidation $scope.form.password, undefined, '...', inverse
validations.push createValidation $scope.form.password, '', '...', inverse

I have an AngularJS app with a form validation that I'm triying to create. There's a function being created to each kind of validation. It is supposed to be passed the $scope.form.input object to each input. But it looks like it's getting passed by value. I really don't know how it works in this kind of JS closure.

Any kind of information would be helpfull.

Hotwer
  • 150
  • 9
  • You declare your validation function with 1 parameter but call it with 0 parameters. Was that intentional? (Although I'm not too familiar with coffeescript so I could be misreading it) – AJ Richardson Apr 22 '15 at 23:26
  • I'm sorry, that's not what it was supposed to be. It was me, getting frustrated. Now it's right. – Hotwer Apr 22 '15 at 23:34

1 Answers1

3

In javascript, you cannot pass simple types (string, numbers, booleans) by reference. As an alternative, you can pass a function that gets the value you are looking for. So for example, instead of passing in $scope.form.termsChecked, you would pass in a function that returns the value of $scope.form.termsChecked.

Here is an example, written in JavaScript because my CoffeeScript isn't too good.

createValidation = function(valueProvider, expected, responseText, inverse) {
    // Skipping a bunch of your code for brevity...
    returningValidation = function() {
        var scopeVaraible = valueProvider();
        console.log(scopeVariable);
        // Now do some validation stuff...
    }
    return returningValidation;
}

validations.push(
    createValidation(function() { return $scope.form.termsChecked; }, true, '...');
AJ Richardson
  • 6,610
  • 1
  • 49
  • 59
  • 1
    Aren't primitives passed by value, and objects passed by reference in JavaScript? – undefined Apr 22 '15 at 23:49
  • It worked, but why just passing the object isn't just enough? – Hotwer Apr 22 '15 at 23:54
  • 1
    When you pass just the value (e.g. `$scope.form.termsChecked`), it creates a copy of the value, and passes the copy to your function. So when you change the original value, the copy doesn't update. – AJ Richardson Apr 22 '15 at 23:56
  • object reference would work if I passed the $scope.form object? – Hotwer Apr 23 '15 at 00:45
  • Correct - if you pass `$scope.form`, then you can modify the *contents* of that object, and they will update. See [this answer](http://stackoverflow.com/a/13104500/1299394). – AJ Richardson Apr 23 '15 at 01:02