私が持っているのは、単純な 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 することで問題が解決する可能性がありますが、現時点ではこれを行う方法がよくわかりません。