0

Problem space

I have a problem where I'm submitting a form based on criteria being fulfilled, rather than having a form submission button.

Let's say I have 3 drop downs, the first two are grouped but one needs to be selected, meaning I can select one or the other but I can't leave them empty, the 3rd one is a required field.

After that, the page automatically fetches in results.

Lets say I have checkboxes and a few more dropdowns. Any future selections on the 3 dropdowns mentioned, checkboxes, and dropdowns automatically filters the results.

What I know

Now after reading angular documentation, I was checking up on $dirty, $pristine and operations on both, like $setDirty and $setPristine; however, it seems that this is for a FormController

So I'm assuming this is useful for an entire scope. I didn't see any inclination that I can figure out for selected scopes.

What I have so far

So basically, I was hoping that I'd be making use of the scope's tracking features, but I don't know much about it. I created a single controller for my application and a single scope, since that's what seemed easiest for me. I have 3rd party plugins that play a role into the scope like:

$scope.3rdpartyConfig = { prop1: [], prop2: getData() }

I don't think something like that would be useful in checking to see form submission if I was going to check the $dirty state of my form.

Then I thought about the old way I used to do things, but "angularlizing" it:

so I'd have something like:

<input type="checkbox" ng-model="state.Checked" ng-change="checkIfWeCanSubmitThenSubmit()" id="ng-change-example1" />

So I'd be having ng-changes and ng-clicks all over my html form, hitting that function, where the function would look like this pseudocode:

$scope.checkIfWeCanSubmitThenSubmit= function() {
    var validated = false;
    //check to see if dropdown1 or dropdown2 are selected
    //check to see if dropdown3 is selected
    // add more here per requirement

    //if the above are true, then validated = true

    if (validated)
    {
         //add dropdown4 and 5, and checkbox groups into filter
    }

    submit();
}

But I was thinking this isn't the angular way of doing things since this certainly isn't facilitated.

I was hoping that the scope would offer some kind of way, where I can check to see what pieces of my scope is dirty or not before I can submit and fetch data, or if there is a better way than appending this function to every html element; like having some kind of scope tracker that I can check up on and watch.

Which reminds me, I don't want to have a series of $scope.$watch either, its just that it'd be way too much work to bind to every piece of html code, unless there's way to watch the scope of a collection of specific scope variables, then, I wouldn't mind.

like (forgive the pseudocode):

$scope.$watch('dropdown1, dropdown2, dropdown4', function(dirty, pristine)
{
    if (dirty)
    { blah blah blah }
});

Edit (2/28/2013):

I tried doing it this way:

$scope.masterCriteria =
[
    { DropDown1: $scope.AppModel.Dropdown1},
    { DropDown2: $scope.AppModel.Dropdown2 },
    { DropDown3: $scope.AppModel.Dropdown3 },
    { Checkbox1: $scope.AppModel.Checkbox1 },
    { Checkbox2: $scope.AppModel.Checkbox2 }
];

$scope.$watch('masterCriteria', function (newVal) {
    if (newVal) { logger.info("did I change?"); }
}, true);

The watcher detected nothing, and any values I changed to the scope of AppModel wasn't being picked up in the $watch. Was worth a try, still trying to figure this out.

sksallaj
  • 3,872
  • 3
  • 37
  • 58

1 Answers1

0

You can slightly change your model and group fields related to input form together. Put them into single object. Like this:

 $scope.state = { checkbox1: false, checkbox2: true, ... }

Later bind input boxes to field of state object:

 <input ng-model="state.checkbox1" ... >

And watch state object to catch all updates of nested fields:

 $scope.$watch('state', ...

JsFiddle example here

Vasiliy Kevroletin
  • 1,188
  • 13
  • 17
  • Interesting, so are you suggesting I make another scope variable, lets say.. readyToSubmit, and have that variable check the data of other scope variables? Then put a watch on readyToSubmit? That actually sounds pretty interesting. This actually seems like a proxy pattern to me. – sksallaj Feb 28 '14 at 18:31