2

私が持っているのは、単純な CRUD 操作です。ユーザーが追加ボタンをクリックすると、アイテムがページにリストされ、モーダルがポップアップし、ユーザーがデータを入力すると、データが保存され、ページのリストに自動的に (更新なしで) 追加されます。

サービス:

getAllIncluding: function(controllerAction, including) {
            var query = breeze.EntityQuery.from(controllerAction).expand(including);
            return manager.executeQuery(query).fail(getFailed);
        }, 

addExerciseAndCategories: function(data, initialValues) {
            var addedExercise = manager.createEntity("Exercise", initialValues);
            _.forEach(data, function(item) {
                manager.createEntity("ExerciseAndCategory", { ExerciseId: addedExercise._backingStore.ExerciseId, CategoryId: item.CategoryId });
            });
            saveChanges().fail(addFailed);

            function addFailed() {
                removeItem(items, item);
            }
        },

コントローラ:

 $scope.getAllExercisesAndCategories = function() {
        adminCrudService.getAllIncluding("ExercisesAndCategories", "Exercise,ExerciseCategory")
            .then(querySucceeded)
            .fail(queryFailed);

    };

     function querySucceeded(data) {

            $scope.queryItems = adminCrudService.querySucceeded(data);

            var exerciseIds = _($scope.queryItems).pluck('ExerciseId').uniq().valueOf();
            $scope.exerciseAndCategories = [];
            var createItem = function (id, exercise) {
                return {
                    ExerciseId: id,
                    Exercise : exercise,
                    ExerciseCategories: []
                };
            };

            // cycle through ids
            _.forEach(exerciseIds, function (id) {
                // get all the queryItems that match
                var temp = _.where($scope.queryItems, {
                    'ExerciseId': id
                });
                // go to the next if nothing was found.
                if (!temp.length) return;
                // create a new (clean) item
                var newItem = createItem(temp[0].ExerciseId, temp[0].Exercise);
                // loop through the queryItems that matched
                _.forEach(temp, function (i) {
                    // if the category has not been added , add it.
                    if (_.indexOf(newItem.ExerciseCategories, i.ExerciseCategory) < 0) {
                        newItem.ExerciseCategories.push(i.ExerciseCategory);
                    }
                });
                // Add the item to the collection
                $scope.items.push(newItem);
            });


            $scope.$apply();

        }

コントローラーから新しいデータを追加する方法は次のとおりです。

 adminCrudService.addExerciseAndCategories($scope.selectedCategories, { Name: $scope.NewName, Description: $scope.NewDesc });

私の質問は、なぜリストがリアルタイムで更新されないのかということです (保存を押すと、ページを更新する必要があります)。

編集

これが私のクエリSucededです

querySucceeded: function (data) {
            items = [];
            data.results.forEach(function(item) {
                items.push(item);
            });
            return items;


        }

編集2

問題を絞り込んだと思います!

PW Kadは、私がこの問題を解決するのを手伝おうとして 2 時間を無駄にしましたが (彼にはとても感謝しています)、残念ながら成功することはありませんでした。私たちは主にサービスを修正しようとしたので、PC に戻ったときに再度修正を試みました。私のサービスは大丈夫だと思います。(カドが彼の答えで提案したように、私はいくつかの変更を加えました)。問題はコントローラーにあると思います.$scope.itemsをログに記録しました.新しいアイテムを追加しても変更されません.その後、$scope.queryItemsをログに記録しました.項目 (更新なし)。したがって、初期データをロードした後に $scope.queryItems を $watching することで問題が解決する可能性がありますが、現時点ではこれを行う方法がよくわかりません。

4

2 に答える 2

3

問題に対処する方法を案内する回答を投稿します。問題は Breeze にも Angular にもないように見えますが、この 2 つを結婚させた方法に問題があるようです。これは、デバッグ プロセスを理解するために何をしているのかを理解することが重要だからです。

エンティティを作成すると、エンティティが isAdded の entityState でキャッシュに追加されます。これは真のステートメントです。そうでないと考えないでください。

今あなたのコードのために...

クエリの実行を約束で連鎖させる必要はありません、あなたの場合、データをコントローラーに返し、それをサービスの関数に戻します。これは質問に記載されていません。あなたがおそらくどのように見えるかを再現する機能を追加しました。

getAllIncluding: function(controllerAction, including) {
            var query = breeze.EntityQuery.from(controllerAction).expand(including);
            return manager.executeQuery(query).then(querySucceeded).fail(getFailed);

            function querySucceeded(data) {
                return data.results;
            }
        }, 

コントローラーで結果を処理するだけです-

$scope.getAllExercisesAndCategories = function() {
    adminCrudService.getAllIncluding("ExercisesAndCategories", "Exercise,ExerciseCategory")
        .then(querySucceeded)
        .fail(queryFailed);
};

 function querySucceeded(data) {
        // Set your object directly to the data.results, because that is what we are returning from the service
        $scope.queryItems = data;  
        $scope.exerciseAndCategories = [];

最後に、エンティティを作成するプロパティを追加して、Angular が適切にバインドする機会を与えるかどうかを確認しましょう -

_.forEach(data, function(item) { 
    var e = manager.createEntity("ExerciseAndCategory"); 
    e.Exercise = addedExercise; e.Category: item.Category; 
});
于 2013-08-17T18:04:19.503 に答える
2

だから私は私の問題を解決することができました!これが正しい解決策かどうかはわかりませんが、現在は機能しています。すべてをサービスに移動しました。現在は次のようになっています。

function addCategoriesToExercise(tempdata) {
        var dataToReturn = [];
        var exerciseIds = _(tempdata).pluck('ExerciseId').uniq().valueOf();
        var createItem = function (id, exercise) {
            return {
                ExerciseId: id,
                Exercise: exercise,
                ExerciseCategories: []
            };
        };

        // cycle through ids
        _.forEach(exerciseIds, function (id) {
            // get all the queryItems that match
            var temp = _.where(tempdata, {
                'ExerciseId': id
            });
            // go to the next if nothing was found.
            if (!temp.length) return;
            // create a new (clean) item
            var newItem = createItem(temp[0].ExerciseId, temp[0].Exercise);
            // loop through the queryItems that matched
            _.forEach(temp, function (i) {
                // if the category has not been added , add it.
                if (_.indexOf(newItem.ExerciseCategories, i.ExerciseCategory) < 0) {
                    newItem.ExerciseCategories.push(i.ExerciseCategory);
                }
            });
            // Add the item to the collection
            dataToReturn.push(newItem);
        });

        return dataToReturn;
    }

    addExerciseAndCategories: function (data, initialValues) {
        newItems = [];

        var addedExercise = manager.createEntity("Exercise", initialValues);

        _.forEach(data, function (item) {
            var entity = manager.createEntity("ExerciseAndCategory", { ExerciseId: addedExercise._backingStore.ExerciseId, CategoryId: item.CategoryId });
            items.push(entity);
            newItems.push(entity);

        });
        saveChanges().fail(addFailed);

        var itemsToAdd = addCategoriesToExercise(newItems);

        _.forEach(itemsToAdd, function (item) {
            exerciseAndCategories.push(item);
        });

        function addFailed() {
            removeItem(items, item);
        }
    }

 getAllExercisesAndCategories: function () {
            var query = breeze.EntityQuery.from("ExercisesAndCategories").expand("Exercise,ExerciseCategory");
            return manager.executeQuery(query).then(getSuceeded).fail(getFailed);

        },

function getSuceeded(data) {
        items = [];
        data.results.forEach(function (item) {
            items.push(item);
        });

        exerciseAndCategories = addCategoriesToExercise(items);

        return exerciseAndCategories;
    }

そしてコントローラーにはこれしかありません:

   $scope.getAllExercisesAndCategories = function () {
        adminExerciseService.getAllExercisesAndCategories()
            .then(querySucceeded)
            .fail(queryFailed);

    };
      function querySucceeded(data) {
            $scope.items = data;

            $scope.$apply();

        }
于 2013-08-18T19:12:51.290 に答える