0

I have followed the js-data angular tutorial to setup js-data-angular in my current project.

model definition:

     .factory('shipmentFactory',
    function(DS) {
      return DS.defineResource({
        name:'ShipmentInstructions',
        idAttribute:'id',
        basePath:'apiEndpoint'
      }
        );
    }).run(function (shipmentFactory){})

In my controller:

.controller('ShipListCtrl', function($scope,shipmentFactory) {
    shipmentFactory.findAll().then(function(shipmentInstructions){
      $scope.shipments = shipmentInstructions;
      console.log($scope.shipments)
    });
});

In my view i attempt to iterate over the returned object using ng-repeat but nothing is displayed. I know that my endpoint is hit and data is returned because my console displays the returned shipment object.

The tutorial mentions how to load data into the views but looks to be for those using ngRoute. I am using ui-router in my current project and ran into more challenges when I tried to resolve that resource in that state.

That resource is used in a view that is included in another view - the template view for the state.

Just looking for some direction on how to get my data to display in my view please and thank you in advance.

Revised shipment model:

  .factory('shipmentFactory',
    function(DS) {
      return DS.defineResource({
        name:'ShipmentInstructions',
         idAttribute:'id',
            basePath:'apiEndpoint',
        cacheResponse: true,
        afterFindAll: function(data, cb){
          //extract the array from that nested property
          shipmentsArray = [];
          //forloop to push every item returned to
          for(var i = 0; i < data.shipmentInstructions.length; i++ ){
            shipmentsArray.push(data.shipmentInstructions[i]);
          }
          cb(null,shipmentsArray)
        }
      }
        );
    }).run(function (shipmentFactory){})/*makes sure shipmentFactory gets defined*/
4

1 回答 1

0

您的发货指令数组嵌套在“shipmentInstructions”属性下,但默认情况下 js-data 期望数据本身是数组。

您需要从该嵌套属性中提取数组。像一个afterFindafterFindAll钩子这样的东西是一个很好的地方。请参阅对另一个 StackOverflow 问题的回答。

于 2015-12-18T18:11:31.457 回答