如何在Angular.js服务中使用Parse JS SDK? $ scope未更新

时间:2015-06-05 06:41:24

标签: javascript angularjs

首先抱歉我的英语
我的任务是创建一个当前的工作报告页面,所以我创建了一个函数getWeek()来获取当前的一周,我必须存储用户输入到Parse数据库 我的问题是我从Parse JS SDK获取数据并存储在$ scope变量中但是$ scope没有更新所以请帮助我.....下面是我的代码

angular.module('workReport', ["xeditable"])
    .run(function(editableOptions) {
        editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
    })
    .factory('Reports', function($q) {
        return {
            getData: function() {
                var defered = $q.defer();
                var data = getWeek();


                var query = new Parse.Query('workReport');

                query.find({
                    success: function(results) {
                        console.log("Successfully retrieved " + results.length + " scores.");

                        // Do something with the returned Parse.Object values
                        for (var i = 0; i < results.length; i++) {
                            var object = results[i];
                            for (var j = 0; j < data.length; j++) {
                                if (data[j].date.getTime() == object.get('date')) {
                                    data[j].activity = object.get('activity');
                                    data[j].mrngSession = object.get('mrngSession');
                                    data[j].afterSession = object.get('afterSession');
                                    data[j].completed = object.get('completed');
                                }
                            }
                        }

                        defered.resolve(data);
                    },
                    error: function(error) {
                        alert("Error: " + error.code + " " + error.message);
                    }
                });
                console.log(defered.promise)
                return defered.promise;
            }
        }
    })
    .controller('WeekReportCtrl', function($scope, Reports) {
        $scope.week = Reports.getData().then(function(r) {
          return r;
        }, function(r) { alert('Failed ' + r) })


    });

function getWeek() {
    var d = new Date
    d = d.toString().split(" ");
    var day = d[0];

    switch (day) {
        case 'Mon':
            return weeks(1, 1, 5);
            break;
        case 'Tue':
            return weeks(2, -1, 4);
            break;
        case 'Wed':
            return weeks(3, -2, 2);
            break;
        case 'Thu':
            return weeks(4, -3, 1);
            break;
        case 'Fri':
            return weeks(5, -4, 0);
            break;
        default:
            console.log('Nothing');
            break;
    }

    function weeks(d, s, e) {
        var week = [];
        for (var i = s; i <= e; i++) {
            var j = new Date;
            j.setHours(0, 0, 0, 0)
            var today = false
            if (i == 0) {
                today = true
            }
            j.setDate(j.getDate() + i);
            week.push({
                date: j,
                activity: "",
                mrngSession: "",
                afterSession: "",
                completed: 0,
                today: today
            });
        };
        return week;
    }
}

1 个答案:

答案 0 :(得分:0)

我想你想做更多的事情:

Reports.getData().then(function(r) {
      $scope.week = r;
}

目前,您正在将Reports.getData()返回的promise对象分配给$ scope.week,而不是传递给then的成功回调函数的参数,这就是您想要的。