Aysha Azura
Aysha Azura

Reputation: 179

Un-check a check-box in angular-meteor

I have done a angular-meteor app, where there are list of categories in my settings.html which are listed with check-box. When it is checked it is displayed in dashboard.html. That is the array from settings collection is pushed to user's collection. But now when I try un-checking it is again pushed to user's collection. So tell me how to remove it from when it is un-checked.

This is my settings.html page

    <div layout-gt-sm="row"  ng-repeat="detail in dynamicSettingsCtrl.generalSettings.generalSetting">
        <md-checkbox aria-label="Checkbox 1"  ng-model="dynamicSettingsCtrl.status[detail._id]" ng-change="dynamicSettingsCtrl.getClicked(detail._id)">

               <p> {{detail.name}} {{ detail.data.cb1 }}</p>
        </md-checkbox>
    </div>

This is my client side js for settings.html

 let self = this;
        let user = Meteor.user();
        let userId = Meteor.userId();
        self.data = {};
        self.data.cb1 = true;
        self.isChecked = true;
        CurrentPage.setTitle('Settings');
        let customerId = [userId]
        self.subscribe('getCustomerDetails', () => [customerId], {
            onReady: function() {
                self.helpers({
                    currentUser: () => {
                        return Meteor.users.findOne({
                            _id: Meteor.userId()
                        });

                    }
                })
            }
        });

        self.subscribe('generalSettingsDetails', () => [])
        this.status = {}
        self.autorun(() => {
                this.currentUser =
                    Meteor.users.findOne({
                        _id: Meteor.userId()
                    });

                this.generalSettings = {}
                this.generalSettings['generalSetting'] = GeneralSettings.find({}).fetch()

                var selectedObs = []
                console.log(self.currentUser.settings)
                console.log(this.generalSettings)

                _.map(this.generalSettings['generalSetting'], function(genset) {
                    delete genset.$$hashKey
                    if (self.currentUser) {
                        let ret = _.find(self.currentUser.settings, function(sets) {
                            return sets._id === genset._id
                        })
                        if(ret){
                            self.status[ret._id] = true
                        }

                    }
                })

            })
            //  console.log(self.currentUser)

        ///subscribing general settings
        // self.subscribe('generalSettingsDetails', () => [], {
        //     onReady: function() {
        //         self.helpers({ /// helpers to make it reactive
        //             generalSettings: () => {
        //                 let settings = GeneralSettings.find({}).fetch()
        //                     //var subSettings = []
        //                 for (key in settings) {

        //                     delete settings[key].$$hashKey

        //                 }
        //                 console.log(settings)
        //                 return {
        //                     'generalSetting': settings
        //                 };
        //             }
        //         })
        //     }


        // });

        ///end of subscribing general settings and starting of click function
        self.getClicked = (settingId) => {
            self.dataArry = {};
            self.dataChk = {};

            // self.currentUser = {};


            console.log(settingId)


            Meteor.call("updateSettings", settingId, function(error, result) {
                if (error) {

                    ToastService.getToastbox($mdToast, 'Something went error!Unable to add details', 'error-toast');
                } else {
                    //self.currentUser = {};

                    ToastService.getToastbox($mdToast, 'Details added successfully !', 'success-toast');

                }
            });


        }

This is my server side js for settings.html

    updateSettings: function(generalValue) {
     let userId = Meteor.userId();
       let settingsDetails = GeneralSettings.findOne({
   "_id": generalValue
 });
            Meteor.users.update({
     _id: userId
   }, {
     $push: {
       "settings": {
       "_id": generalValue,
       "name": settingsDetails.name,
       "description": settingsDetails.description,
       "tag": settingsDetails.tag,
        "type": settingsDetails.type,
       "status": settingsDetails.status
   }
     }


   })

}

How can i do deleting?How to remove that tick when trying to uncheck

Upvotes: 0

Views: 64

Answers (1)

Gyandip Pandey
Gyandip Pandey

Reputation: 21

Right now, you are calling the same function on every change on checkbox (which only pushes on finding by it's id) . It calls the same update method and you haven't restricted the method of unchecked or checked condition.

Only call getClicked function when checked is true, return false otherwise.

Upvotes: 0

Related Questions