在服务角更新变种 [英] Angular updating var in service

查看:101
本文介绍了在服务角更新变种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个服务,在那里我可以检查是否有建筑物被更新或没有。

I'm trying to make a service, where I can check if an building is updated or not.

所以我有以下设置为中的service

so I have the following setup for the serivce

define(['app'], function (app) {

    'use strict';
    app.service('BuildingService', function ($http, $q, $rootScope) {
        var buildings = null;
        var dirtyBuildings = [];
        var originalBuildings = [];


        this.getBuilding = function(id) {
            var deferred = $q.defer();
            // check if buildings were set
            if (buildings == null) getBuildings();

            // get index
            var index = arrayObjectIndexOfBuilding(buildings, { BuildingId: id });
            if (index < 0) deferred.reject("Building doesn't exist");
            // return object
            deferred.resolve(buildings[index]);

            return deferred.promise;
        }
        this.getBuildings = function () {
            var deferred = $q.defer();
            if (buildings == null) {
                $http.get('http://ws33177.sidmar.be/FMMService/api/Building').success(function (data) {
                    buildings = data;
                    deferred.resolve(buildings);
                }).error(function () {
                    deferred.reject();
                });
            } else {
                deferred.resolve(buildings);
            }
            return deferred.promise;

        }


        this.saveBuildings= function() {
            // TODO: Save logic
        }

        this.updateBuilding = function (newBuilding) {
            console.log("Updating");

            var deferred = $q.defer();

            // check if building exists in list
            var index = arrayObjectIndexOfBuilding(buildings, newBuilding);
            if (index < 0) deferred.reject("Building doesn't exist");
            console.log(newBuilding, buildings[index]) // <--- referenced in text

            // check if updated
            if (angular.equals(buildings[index], newBuilding)) {
                console.log("Same object");

            } else {
                // check if already dirty or not
                var dirtyIndex = arrayObjectIndexOfBuilding(dirtyBuildings, newBuilding);
                if (dirtyIndex < 0) {// new dirty
                    console.log("Something changed, new dirty");
                    // save in dirty list
                    dirtyBuildings.push(newBuilding);
                    // save original
                    originalBuildings.push(buildings[index]);
                } else {// already dirty
                    var originalIndex = arrayObjectIndexOfBuilding(originalBuildings, newBuilding);

                    // check if isn't reverted
                    if (angular.equals(originalBuildings[originalIndex], newBuilding)) {
                        console.log("Back to original");
                        // if same, remove from dirty buildings
                        dirtyBuildings.splice(dirtyIndex, 1);
                        // set old back
                        buildings[index] = originalBuildings[originalIndex];
                    } else {
                        console.log("Something changed, updating dirty");

                        // update the dirty building with latestd changes
                        dirtyBuildings[dirtyIndex] = newBuilding;
                    }
                }

                // update buildinglist
                buildings[index] = newBuilding;
            }

            $rootScope.$broadcast('updatedBuilding', { totalDirty: dirtyBuildings.length });
            deferred.resolve();

            return deferred.promise;
        }

        function arrayObjectIndexOfBuilding(arr, obj) {
            if (arr == null || obj == null) return "nothing";

            for (var i = 0; i < arr.length; i++) {
                if (arr[i].BuildingId == obj.BuildingId) {
                    return i;
                }
            }

            return -1;
        };
    });

    return app;
});

然后我通过获得建筑:

then I get the buildings via:

BuildingService.getBuilding(page.options.BuildingId).then(function (result) {
    $scope.building = result
    $scope.$watch('building', function (newVal) {
        BuildingService.updateBuilding(newVal);
    }, true);
}, function () {
    ons.notification.alert({ message: 'An error has occurred!' });
});

建筑有一个叫做分数值(所以 $ scope.building.score ),但是当我更新的价值,在建筑物原值在我服务 VAR已经更新。所以chcek总是返回同一个对象

Building has a value called score (so $scope.building.score) but when I update that value, the original value in the buildings var in my service is already updated. So the chcek always returns Same object.

什么可能我是做错了?

推荐答案

您正在创建每次调用 BuildingService.getBuildings 方法时,一个新的手表。
而不是创建打包票回调外的手表。

You are creating a new watch each time you call the BuildingService.getBuildings method. instead create the watch outside of the promise success callback.

$scope.property = 'score';
var buildingWatch;
BuildingService.getBuilding(page.options.BuildingId).then(function (result) {
    $scope.building = result;
}, function () {
    ons.notification.alert({ message: 'An error has occurred!' });
});


$scope.$watch('property', function (newVal) {
    if (buildingWatch) {
      buildingWatch();
    }
    buildingWatch = $scope.$watch('building.' + newVal , function (newVal) {
      BuildingService.updateBuilding(newVal);
    }, true);
    BuildingService.updateBuilding(newVal);
}, true);

如果你想观看动态创建的属性我建议建立在另一个新的手表,每一次删除该属性的更改

If you want to watch for dynamically created properties I suggests create a new watch inside another and remove it each time the property changes

这篇关于在服务角更新变种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆