0

I've been working on a requirement for a one-many-one relationship in Angular and Breeze and I've tried to apply Ward Bell's recommendation from this post in my solution so far. To see the code I'm using so far, please visit my original question and check the answer I submitted myself.

The problem, I'm now facing is when I want to prepare for saveChanges. The reason being, is that I have used nested controllers. The function that calls the save is in my parent controller and the property that holds my affected one-may-one entities is on the child controller.

In the parent controller...

function save() {
        if (!canSave()) {
            return $q.when(null);
        }

        // CODE TO PREPARE THE ONE-MANY-ONE ENTITIES FOR
        // SAVING SHOULD GO HERE I'M GUESSING...

        vm.isSaving = true;
        return datacontextSvc.save().then(function(saveResult) {
            vm.isSaving = false;
            trapSavedDboardConfigId(saveResult);
        }, function(error) {
            vm.isSaving = false;
        });
    }

and in the child controller...

function getBusUnits() {
    ...
    .then(function(data){
        vm.busUnits = data;
        vm.busUnits.forEach(function(bu){
            getBusUnitChannels(bu);
        });
    });
}

function getBusUnitChannels(busUnit) {
        datacontextSvc.dboardConfigs.getBusUnitChannelsById(busUnit.id)
            .then(function (data) {
                busUnit.busUnitChannelsList = data;

                // THE PROBLEM IS THAT THE BUCHANNELS ARRAY IS CREATED
                // FOR EACH BUSUNIT AND THE LIST OF BUSUNITS IS ONLY 
                // CALLED IN THE CHILD CONTROLLER.
                // DOES THIS IMPLY I CAN'T HAVE A PROPERTY IN THE PARENT CONTROLLER
                // THAT I CAN REFERENCE IN THE CHILD, SO THAT THE PARENT CAN ACCESS
                // THE DATA DURING SAVE?

                busUnit.buChannels = []; // HOW DO I DEFINE THIS GUY IN THE PARENT?
                vm.channels.forEach(function (channel) {
                    busUnit.buChannels.push(channel);
                });

                busUnit.busUnitChannelsList.forEach(function (buc) {
                    busUnit.buChannels.forEach(function (buCh) {
                        if (buc.channelId === buCh.id) {
                            buCh.buChannel = buc;
                            buCh.isSelected = true;
                        } else {
                            buCh.isSelected = false;
                        }
                    });
                });
            });
    }

The source of the problem is that the call for the one-many-one entity and the creation of an associated item viewmoder array (buChannels) only occurs after i've called for busUnits, and the call for busUnits happens in the child controller. Also, the buChannels array needs to be stored for each busUnit.

Because Angular parent controllers can't read child properties, the normal solution would be to create a property in the parent and reference it in the child. But, since the property I'd want to reference is dependent on an entity that's only fetched in the child, this can't be done.

Or can it? Any ideas or other recommended approaches. I'd really like to avoid only having a parent controller...

Community
  • 1
  • 1
zpydee
  • 4,487
  • 4
  • 16
  • 19
  • possible duplicate of [Angular Breeze one-many-one example with checkbox](http://stackoverflow.com/questions/21475998/angular-breeze-one-many-one-example-with-checkbox) – PW Kad Feb 01 '14 at 15:27
  • not a duplicate, an extension thereof. first part of my question dealt with displaying one-many-one results. this question deals with saving in a parent-child controller scenario. – zpydee Feb 01 '14 at 15:44
  • I read your question as propagating child controller changes to parent but i could be wrong. If not though, would this already answered question work for your case: http://stackoverflow.com/questions/16972976/angular-js-propagate-child-scope-variable-changes-to-parent-scope? – nymo Feb 01 '14 at 21:15

0 Answers0