2013-01-10 13 views
4

我有一個MongoDB的文件(上MongoLab.com)呼籲client,這將被用於跟蹤健身比賽。每週都會有一個「權衡」,並跟蹤其他一些細節。添加一個新的模式場MongoLab的AngularJS方式

我卡在什麼是如何將子文檔添加到client文檔跟蹤使用AngularJS結果。

我覺得這可能是一些淺顯的我失蹤,但我還沒有想出如何一個新的領域追加到現有的記錄,陣列或非陣列。

下面是我認爲的相關細節,但隨時如果合適的話,要求更多的細節。

全部代碼在http://pastebin.com/4tyRiKus

實施例的客戶端粘貼從經由_id提取數據不同的模板內(I可以在沒有麻煩添加客戶端)

{ 
    "_id": { 
     "$oid": "50eee69fe4b0e4c1cf20d116" 
    }, 
    "firstName": "Jon", 
    "lastName": "Doe" 
} 

查閱/細節/:cliendId)哪些工作,但我想添加一個數據點的權衡。

<form ng-submit="addWeighIn()"> 
    <input type="date" ng-model="date" required /> 
    <input type="text" ng-model="weight" placeholder="Weight" smart-float required /> 
    <input type="submit" class="btn btn-primary" value="Add" /> 
</form> 

點擊添加這裏按鈕,在接下來的部分繼續進行。

在控制處理功能(這確實火和MongoLab模塊中調用更新())

$scope.addWeighIn = function() { 
    console.log("addWeighIn: " + $scope.date + " : " + $scope.weight); 
    // the below line is obviously wrong, just out of ideas. 
    $scope.client.weights = [ { date: $scope.date, weight: $scope.weight } ]; 
    $scope.client.update(function() { 

    }); 
}; 

控制檯顯示addWeighIn線與所述網絡的數據顯示,它發送一個請求,但它在文檔中實際上並不重要,並且與上面相同。

從$ scope.addWeighIn(稱爲MongoLab資源)和火災

Client.prototype.update = function (cb) { 
    console.log("mongolab: update: " + cb); 
    return Client.update({ id: this._id.$oid }, 
     angular.extend({}, this, { _id: undefined }), cb); 
}; 

通過更新()發送(缺少新的稱重數據)數據

{ 
    "_id": { 
     "$oid": "50eee69fe4b0e4c1cf20d116" 
    }, 
    "firstName": "Jon", 
    "lastName": "Doe" 
} 

這是我正在尋找,在實際的數據庫中,但我需要添加權重,並認爲我不理解的基本事情。

{ 
    "_id": { 
     "$oid": "50eee69fe4b0e4c1cf20d116" 
    }, 
    "firstName": "Jon", 
    "lastName": "Doe" 
    "weights": [ 
     { "date": "1/3/2013", 
      "weight": 155 } 
     { "date": "1/10/2013", 
      "weight": 150 } 
    ] 
} 

我有幾個問題要怎麼做。

  1. 我可以在HTML代碼做這個聲明,如設置ng-modelclient.weights.date?例如:<input type="date" ng-model="client.weights.date" required />(不工作,但我看到的,如果有一個類似的方式)
  2. 如果沒有聲明,什麼是通過AngularJS這樣做的正確方法?
  3. 我應該用什麼樣的術語來找到這方面的東西?這似乎是我的主要挫折,找不到例子。

我發現$推動在http://docs.mongodb.org/manual/applications/update/描述,可能是什麼,我需要使用,但我不能確定如何使用該的AngularJS方式

+0

client.weights因此陣列你需要做一個NG-重複= 「重量client.weights」 並結合該變量to ng-model =「weight.data」 –

+0

'client.weights'中還沒有任何數據。我希望知道如何將數據實際存入MongoDB文檔 – Kirk

回答

3

我最終發現我的問題是與MongoLab資源。

我用我在https://github.com/pkozlowski-opensource/angularjs-mongolab找到的那個傷心的實現進行了調換,它工作正常。

由於我努力尋找一個例子,下面是如何添加子文檔。

HTML

<form ng-submit="addWeighIn()"> 
    <table class="table table-striped"> 
     <thead> 
     <tr> 
      <th>Date</th> 
      <th>Weight</th> 
      <td></td> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="weight in client.weighIn | orderBy:'date'"> 
      <td>{{weight.date}}</td> 
      <td>{{weight.weight}}</td> 
      <td></td> 
     </tr> 
     </tbody> 
     <tfoot> 
     <tr> 
      <td> 
       <input type="text" ng-model="date" placeholder="Date of weigh-in" required /> 
      </td> 
      <td class="input-append"> 
       <input type="text" ng-model="weight" placeholder="Weight" smart-float required /> 
       <span class="add-on">lbs</span> 
      </td> 
      <td><input type="submit" class="btn btn-primary" value="Add" /></td> 
     </tr> 
     </tfoot> 
    </table> 
</form> 

腳本

$scope.addWeighIn = function() { 
    console.log("addWeighIn: " + $scope.date + " : " + $scope.weight); 
    $weighIn = { date: $scope.date, weight: $scope.weight }; 
    // push to the existing array if it exists 
    if ($scope.client.weighIn) 
     $scope.client.weighIn.push($weighIn); 
    // else create the field if it doesn't exist 
    else 
     $scope.client.weighIn = [ $weighIn ]; 

    $scope.client.update(function() { 
     // do whatever after the update 
    }); 
}; 
相關問題