1

The commented part will lie under $scope.$on. I need to return options to the place where I have kept $scope.$emit. Please HELP!!!

if (gridConfig.Batch) {
                gridOption.dataSource.transport["parameterMap"] = function (options, operation) {
                    var data = {
                        options: options,
                        operation: operation
                    };
                    $scope.$emit('parameterMap', data);
                    //if (operation !== "read" && options.models) {
                    //    angular.forEach(options.models, function (value) {
                    //        value.MfgDt = kendo.toString(value.MfgDt, "s");
                    //        value.ExpDt = kendo.toString(value.ExpDt, "s");
                    //        value.ProductType = value.ProductType.Id;
                    //    })
                    //    return options;
                    //}
                }
            }
  • Even if there is any other way to do this then let me know. Also this code is under angular custom directive which is being used in many places so I can't keep the commented code there. It has to be passed from somewhere outside. – Alisha Chaitali May 20 '17 at 10:25
  • Do you want to send data from $scope.$emit to $scope.$on ? – Amadou Beye May 20 '17 at 13:00
  • @AmadouBeye Yes.. I need that data – Alisha Chaitali May 20 '17 at 14:52
  • Okay take a look at this post: http://stackoverflow.com/questions/14502006/working-with-scope-emit-and-scope-on – Amadou Beye May 20 '17 at 15:24
  • I understood you wrong. I want to send data from $scope.$on to $scope.$emit... @AmadouBeye – Alisha Chaitali May 20 '17 at 16:01
  • I already came across that post. But that was not helpful. – Alisha Chaitali May 20 '17 at 16:01
  • Someone please Help!!! I need to deliver this tomorrow morning – Alisha Chaitali May 21 '17 at 05:20
  • What exactly is the problem? are you able to transfer the data at all? and can you share your $scope.$on code? – Gal Grazia May 21 '17 at 07:30
  • This code is a part of angular directive which is made for handling all the grids in my project. If it's not a directive then the code will look this – Alisha Chaitali May 21 '17 at 12:06
  • `parameterMap: function (options, operation) { if (operation !== "read" && options.models) { angular.forEach(options.models, function (value) { value.Mfg_Dt = kendo.toString(value.Mfg_Dt, "s"); value.Exp_Dt = kendo.toString(value.Exp_Dt, "s"); value.Product_Type = value.Product_Type.Id; }) return options; } } ` – Alisha Chaitali May 21 '17 at 12:09
  • My $scope.$on: `$scope.$on('parameterMap',function(e, data){ return function(){ if (data.operation !== "read" && data.options.models) { angular.forEach(data.options.models, function (value) { value.Mfg_Dt = kendo.toString(value.Mfg_Dt, "s"); value.Exp_Dt = kendo.toString(value.Exp_Dt, "s"); value.Product_Type = value.Product_Type.Id; }) return data.options; } } });` – Alisha Chaitali May 21 '17 at 12:13
  • I want to return that data.options to the place I kept my $scope.$emit In case you have any other way then please let me know @GalGrazia – Alisha Chaitali May 21 '17 at 12:14
  • @AlishaChaitali I think i know what you mean, i posted an answer, please tell me if that is what you wanted to achieve – Gal Grazia May 21 '17 at 12:17
  • @GalGrazia Exactly this is what I want – Alisha Chaitali May 21 '17 at 12:22
  • Thank you so much @GalGrazia .Worked like charm. I didn't knew we can use callbacks with emit and on – Alisha Chaitali May 21 '17 at 12:35
  • 1
    I already did that. But it's saying I need t have more than 15 reputation to accept your answer. Give me sometime.I ll increase my reputation and upvote your answer again – Alisha Chaitali May 21 '17 at 12:38
  • 1
    @GalGrazia I upvoted your answer. Thanks for the help – Alisha Chaitali May 22 '17 at 02:53
  • @AlishaChaitali i saw you got an answer already :) – Gal Grazia Jun 12 '17 at 07:13

2 Answers2

4

I'm not sure i understand your question but if i do, you wanted a callback function from your $scope.$on to your $scope.emit. If so, the code below will help you, please tell me if it's working because i haven't tested it.

if (gridConfig.Batch) {
            gridOption.dataSource.transport["parameterMap"] = function (options, operation) {
                var data = {
                    options: options,
                    operation: operation
                };
                $scope.$emit('parameterMap', data,function(returnedData){
                      //returnedData.options = the updated options
                });

            }
        }

$scope.$on:

$scope.$on('parameterMap', function (e, data,callback) {
    if (data.operation !== "read" && data.options.models) {
        angular.forEach(options.models, function (value) {
            value.MfgDt = kendo.toString(value.MfgDt, "s");
            value.ExpDt = kendo.toString(value.ExpDt, "s");
            value.ProductType = value.ProductType.Id;
        })
        callback(data);
    }

})
Gal Grazia
  • 477
  • 6
  • 9
0

I think you can make it far simpler than the other answer, just adjust the data in the directive, and use that as the return value.

if (gridConfig.Batch) {
            gridOption.dataSource.transport["parameterMap"] = function (options,   operation) {
                var data = {
                    options: options,
                    operation: operation
                };
                $scope.$emit('parameterMap', data);

                // by the time we reach here the directive 
                // will have altered the data object
                if (data.operation !== "read" && data.options.models) {
                    angular.forEach(options.models, function (value) {
                        value.MfgDt = kendo.toString(value.MfgDt, "s");
                        value.ExpDt = kendo.toString(value.ExpDt, "s");
                        value.ProductType = value.ProductType.Id;
                    })
                    return options;
                }
            }
        }

in the directive:

$scope.$on('parameterMap', function (e, data) {
   data.operation = "write" ; 
   data.options = {models: 'something here'} 
   // do something else here, whatever the purpose of the directive is
}
wobbily_col
  • 11,390
  • 12
  • 62
  • 86