2014-02-10 49 views
4

我是angular/ui-route和firebase中的新成員。使用ui-routes解析firebase數據

你知道是否可以使用ui-route解析firebase數據?

我tryed以下狀態:

 .state('contacts', { 

     abstract: true, 
     url: '/contacts', 
     templateUrl: './assets/app/views/contacts/contacts.html', 
     resolve: { 
      contacts: ['contacts', 
      function(contacts){ 
       return contacts.all(); 
      }], 
      contactsFb: WHAT TO SET ??? 
     }, 

     controller: ['$scope', '$state', 'contacts', 'utils', 'contactsFb', 
      function ( $scope, $state, contacts, utils, contactsFb) { 

      // Working Fine but not from firebase 
      $scope.contacts = contacts; 
      // Can't make it works... :-(
      $scope.contacts = contactsFb; 

      }] 
     }) 

這裏是工廠:

.factory('contactsFb', function($firebase) { 
    var url='https://evet.firebaseio.com/contacts'; 
    return $firebase(new Firebase(url)); 
    }) 

    .factory('contacts', ['$http', function ($http, utils) { 
    var path = './assets/app/models/contacts.json'; 
    var contacts = $http.get(path).then(function (resp) { 
     return resp.data.contacts; 
    }); 

    var factory = {}; 
    factory.all = function() { 
     return contacts; 
    }; 
    factory.get = function (id) { 
     return contacts.then(function(){ 
     return utils.findById(contacts, id); 
     }) 
    }; 
    return factory; 
    }]) 

不能讓它工作... :-( 也許你可以幫我

非常感謝!

回答

5

嘗試類似這樣:

.state('contacts', { 

    abstract: true, 
    url: '/contacts', 
    templateUrl: './assets/app/views/contacts/contacts.html', 
    resolve: { 
    loadContacts: function(contactsFb) { 
     return contactsFb.promiseToHaveContacts(); 
    } 
    }, 

    controller: function ($scope, contactsFb) { 
     $scope.contacts = contactsFb.contacts; 
    } 
}) 

.factory('contactsFb', function($firebase, $q) { 
    return { 
    contacts: null, 
    promiseToHaveContacts: function() { 
     var deferred = $q.defer(); 

     if (this.contacts === null) { 
     this.contacts = $firebase(new Firebase('https://evet.firebaseio.com/contacts')); 
     this.contacts.$on('loaded', function(loadedData) { 
      deferred.resolve(); 
     }); 
     } 
     else { 
     deferred.resolve(); 
     } 

     return deferred.promise; 
    } 
    }; 
}); 
+0

我搶!非常感謝您的回覆。它的工作正常... –

+0

但實際上聯繫人詳細顯示不再工作。我需要使用utils.findById等其他方法(如all()或get(),並且我無法設置匹配語法:-( –

+0

您能澄清什麼不起作用嗎? – rob