如何从parse.com获取数据并使用AngularJs将其呈现给表

时间:2014-09-18 22:45:46

标签: collections parse-platform promise

我正在尝试获取存储在parse.com集合中的数据。我正在使用Parse Javascript SDK以异步方式调用服务,如下所示:

    ctrl.factory('TLDs', function($q){

    var query = new Parse.Query(Fahras)// Fahras is the Parse Object initialized earlier in code
    query.equalTo("type", "Domain")
    var myCollection = query.collection()

    return {
        fetchDomains: function(){
            var defer = $q.defer();

            myCollection.fetch({
                success : function(results) {
                    defer.resolve(results.modles);
                    console.info(results.models)
                },
                error : function(aError) {
                    defer.reject(aError);
                }
            });
            console.info(defer.promise)
            return defer.promise;

        }
    }
}) // end of factory topDomains

我有一个简单的表来显示提取的数据

<div id="showdomainshere"> {{domains}}</div>

    <table id="domains_table" class="table table-hover">
        <thead>
        <tr>
            <th>Domain</th>
            <th>Code</th>
            <th>Subjects</th>
            <th>Instances</th>
        </tr>
        </thead>
        <tbody id="table_body">
        <form id="edit_row" class="form-inline">

                <tr ng-repeat="item in domains">
                    <td><span>{{item.attributes.arTitle}}</span>
                    </td>

                    <td><span>{{item.attributes.domainCode}}</span>
                    </td>

                    <td><span>{{subclasses}}</span>    
                    </td>

                    <td><span>{{instances}}</span>
                    </td>
                </tr>

        </form>

        </tbody>
    </table>

</div> <!-- end of main div -->

以下是我用来渲染视图的控制器:

ctrl.controller('Home', ['$scope','TLDs',function($scope, TLDs) {

    $scope.domains = TLDs.fetchDomains()


}])

使用console.info我可以看到结果已被提取,我可以按预期查看返回模型的数组。问题是$ scope.domains从未更新过,因此表从未呈现过

1 个答案:

答案 0 :(得分:0)

幸运的是,我能够弄明白。

控制器应该像:

ctrl.controller('Home', ['$scope','TLDs',function($scope, TLDs) {
    TLDs.fetchDomains().then(function(data){
        $scope.domains = data
    })
}

虽然工厂本身应该像:

ctrl.factory('TLDs', function($q){
    var query = new Parse.Query(Fahras)
    query.equalTo("type", "Domain")
    var myCollection = query.collection()

    return {
        fetchDomains: function(){
            var defer = $q.defer();

            myCollection.fetch({
                success : function(results) {
                    defer.resolve(results.models)
                    return results.models
                },
                error : function(aError) {
                    defer.reject(aError)
                }
            })

            return defer.promise;

        }
    } }) // end of factory