0

I have created a custom method update with method type put.

I want to catch the success and fire some event there.

Does anyone have fair idea how to do it?

SERVICE

(function () {
'use strict';
 // this function is strict...

angular
    .module('zdma.organization.services', [])
   .factory('Organization', function($resource, Config) {

        var Organization = $resource(
                Config.url.organization + '/:id',
            {
                id: '@id' 
            },
            {
                'update': {
                    method: 'PUT'
                }
            }
        );

        // class methods
        Organization.getEmptyOrganizationTemplate = function () {
            return {
                'id': '',
                'name': '',
                'declarationCode': '',
                'website': ''
            };
        };
        return Organization;
    });
}());

In controller

angular
    .module('zdma.organization.controllers', [])
.controller('OrganizationCreateCtrl', function($scope, $location, Organization) {
    function init() {
        $scope.organization = new Organization(Organization.getEmptyOrganizationTemplate());
    }

    $scope.save = function() {   
        $scope.organization.$save();
    };
init();
});

Can you help me how can i do it in this case?

Please assume that my model updates on the base of view

Aditya Sethi
  • 10,486
  • 11
  • 26
  • 31

1 Answers1

0

Controller

angular.module('zdma.organization.controllers', [])
.controller('OrganizationCreateCtrl', function($scope, $location, Organization) {
    function init() {
        $scope.organization = new Organization(Organization.getEmptyOrganizationTemplate());
    }
    $scope.save = function() {   
        $scope.organization.$update(
           /* this function will be fired if you have a success */ 
           function(data){
               /* Put your events here */
           },
           /* this funciton will be fired if you have a failure */
           function(error){}
        );
    };
    init();
});
Marwen Cherif
  • 560
  • 5
  • 13
  • Updated my question, please have a look – Aditya Sethi Aug 26 '14 at 12:51
  • @AdityaSethi hope it helps. – Marwen Cherif Aug 26 '14 at 13:45
  • I am using proper injection buddy. I have injected Organization service in my controller. Why cant i use $scope.organization = new Organization($scope.editModel); and then apply update with callback? I am not able to actually get update method :( $scope.organization.$update(); works fine, but only with refresh. That is why i want to redirect my page only on success which I am not able to find :( – Aditya Sethi Aug 26 '14 at 13:56
  • replace : $scope.organization.$update(); with : $scope.organization.update({},{},function(data){ /* Put your code her / }, function(error){ / Put your code her */ ); – Marwen Cherif Aug 26 '14 at 14:07
  • @AdityaSethi like this. – Marwen Cherif Aug 26 '14 at 14:31
  • $scope.organization.$update( /* this function will be fired if you have a success */ function(data){ /* Put your events here */ $location.path('organization/list'); }, /* this funciton will be fired if you have a failure */ function(error){} ); doesnt help – Aditya Sethi Aug 26 '14 at 14:39