2013-08-16 64 views
2

我擁有的是簡單的CRUD操作。項目列在頁面上,當用戶點擊按鈕添加,模式彈出,用戶輸入數據,數據被保存,應自動(不刷新)被添加到頁面上的列表。Angularjs必須刷新頁面才能看到更改

服務:

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 }); 

列表實時(當我保存時我必須刷新頁面)。

編輯

這裏是我的querySuceeded

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


     } 

EDIT 2

我相信我已經縮小我的問題!

因此PW Kad因爲我試圖幫助我解決這個問題而失去了兩個小時(廣告我非常非常感謝他),但不幸的是沒有成功。我們主要試圖修復我的服務,所以當我回到我的電腦時,我再次嘗試修復它。我相信我的服務很好。 (我在Kad建議的答案中做了一些修改)。 我相信問題是在控制器中,我記錄了$ scope.items,並且當我添加新項目時它們不會更改,之後我記錄了$ scope.queryItems,並且我注意到它們在添加新項目(不刷新)。所以可能的問題將通過$加載初始數據後$ $ scope.queryItems來解決,但目前我不太確定如何執行此操作。

+1

正如擡起頭,那是絕對噸碼你發佈了這些內容,並且仔細研究它是非常耗時的。如果您將其降至您遇到問題的代碼,確定問題發生的位置並尋求特定幫助,您可能會獲得更多成功。 –

+0

將這些代碼放入在線演示中以節省您的時間和時間會更有用。 – zsong

+0

感謝您的建議,我會這樣做 – hyperN

回答

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(); 

     } 
3

好的,我打算髮佈一個答案,指導您如何解決您的問題。這個問題似乎不是與微風,也不是與Angular相關的,而是你們兩人結婚的方式。我這樣說是因爲了解你在做什麼以瞭解調試過程非常重要。

創建實體將其添加到緩存中,並添加了isAdded的實體狀態 - 這是一個真實的陳述,不要以其他方式。

現在爲您的代碼...

你不到鏈中的一個諾言查詢執行,但在你的情況是將數據返回到控制器,然後通過它的右後衛在您的問題中沒有列出您的服務中的某些功能。我添加了一個函數來複制你的可能是的樣子。

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 = []; 

最後,讓我們添加我們創造的實體和查看屬性,如果,讓角一個正確綁定了機會 -

_.forEach(data, function(item) { 
    var e = manager.createEntity("ExerciseAndCategory"); 
    e.Exercise = addedExercise; e.Category: item.Category; 
}); 
相關問題