Organizations service to return rows for typeahead
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Jan 2013 18:32:57 +0000 (19:32 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Jan 2013 18:32:57 +0000 (19:32 +0100)
app/js/controllers.js
app/js/services.js.ep

index 1452752..2440bae 100644 (file)
@@ -2,12 +2,15 @@
 
 /* Controllers */
 
-
-function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, View) {
+function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $route, Organizations) {
 
        $scope.$routeParams = $routeParams;
        $scope.$location = $location;
        $scope.organizations = [];
+       Organizations.getArrayPromise().then(function(data) {
+               $scope.organizations = data;
+               $log.info('organizations promise ', $scope.organizations);
+       });
 
        $scope.update = function(registration, state) {
 
@@ -47,26 +50,7 @@ function RegistrationCtrl($scope, $log, Registration, $routeParams, $location, $
 
                $log.info( $routeParams.registrationId );
 
-               $scope.view = new View();
-if(0){//FIXME
-               $scope.view.$get({ view: 'organizations' }, function(result) {
-                       $log.info('View organizations', result);
-                       angular.forEach( result.rows, function(value, key) {
-                               this.push( value.key ); // name of organization
-                       }, $scope.organizations );
-                       $log.info('organizations', $scope.organizations);
-               });
-}else{//FIXME
-               $scope.view.$key_array({ view: 'organizations' }, function(result) {
-                       $log.info('key_array', result);
-//                     $scope.organizations = result.rows; // FIXME doesn't refresh bs-typeahead
-                       angular.forEach( result.rows, function(o) {
-                               this.push( o );
-                       }, $scope.organizations );
-                       $log.info('organizations', $scope.organizations);
-               });
        }
-}//FIXME
 
        $scope.$watch('user.registration_type', function( oldValue, newValue ) {
                $log.info("registration_type watch", oldValue, newValue );
@@ -161,7 +145,7 @@ if(0){//FIXME
 
        $scope.reset();
 }
-//RegistrationCtrl.$inject = [ '$scope', '$log' ];
+RegistrationCtrl.$inject = [ '$scope', '$log', 'Registration', '$routeParams', '$location', '$route', 'Organizations' ];
 
 function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
 
@@ -227,6 +211,4 @@ function ListCtrl($scope, $log, Registration, RegistrationTypes, $filter) {
 }
 
 
-function MyCtrl2() {
-}
-MyCtrl2.$inject = [];
+ListCtrl.$inject = [ '$scope', '$log', 'Registration', 'RegistrationTypes', '$filter' ];
index 45d5b37..6ad0b9a 100644 (file)
@@ -43,9 +43,26 @@ angular.module('myApp.services', [ 'ngResource' ]).
                        query: {method:'GET', params:{registraionId:'@id'}, isArray:true}
                });
        }).
-       factory('View', function($resource) {
-               return $resource('/:database/_design/registration/_view/:view?group=true;format=:format', { database: '<%= $couchdb_database %>' }, {
-                       key_array: {method:'GET', params:{ format:'key_array' }, isArray:false}
-               });
+/* http://jsfiddle.net/asgoth/7bNAd/ */
+       factory('Organizations', function($q, $http, $log) {
+               var url = '/<%= $couchdb_database %>/_design/registration/_view/organizations?group=true;format=key_array;callback=JSON_CALLBACK';
+
+               return {
+                       getArrayPromise: function() {
+                               var deferred = $q.defer();
+
+                               $log.info('defer', url);
+
+                               $http.jsonp(url).success(function(json) {
+                                       $log.info('success', url);
+                                       deferred.resolve(json.rows);
+                               }).error(function(error) {
+                                       $log.error('error', url, error );
+                                       deferred.reject(error);
+                               });
+                               return deferred.promise;
+                       }
+               };
 
+               return $scope.organizations;
        });