Converting business object to viewmodel in angular meteor

Background

I have an angular -meteor app and a set of business objects in mongodb, for example:

{ startDate: new Date(2015, 1, 1), duration: 65, value: 36 }

      

I want to display this data in different views. One type is a graph, the other is a list of records. The list of entries is simple. Just bind the collection to the model:

vm.entries = $scope.meteorCollection(MyData, false);

      

In the view, I would do:

<div ng-repeat="entry in vm.entries">{{entry.startDate}} - ...</div>

      

But now for the schedule. I want to convert each item to an object { x, y }

and provide a view, for example:

vm.graphPoints = transformData(getDataHere());

      

The problem is that no data is selected here, in angular-meteor it looks like it is being called when called entry in vm.entries

in the view. The kicker is that the method transformData

must loop through the data and for each index of an element into other elements to calculate the x, y obtained. Therefore, I cannot use a simple forEach loop (without any access to other elements).

Question

So how can I get the data in the controller - transform it - and still have one-way binding (observation) to the new data added to the database?

thank

Update

The following code works, but I think there might be problems loading the entire collection every time there are changes.

$scope.$meteorSubscribe("myData").then(function() {
    $scope.$meteorAutorun(function() {
        var rawData = MyData.find().fetch();
        vm.model.myData = transformData(rawData);
    });
});

      

+3


source to share


2 answers


EDIT:
This is the current solution:

$scope.collectionData = $scope.meteorCollection(MyData, false);
$meteor.autorun($scope, function() {
        vm.graphPoints = transformData($scope.collectionData.fetch());
    });

      



There is some missing information. don't you want to have some model object in the client? if it's correct i think you need to do something like this:

$meteor.autorun($scope, function() {
        vm.graphPoints = transformData($scope.meteorCollection(MyData, false));
    });

      

+1


source


How about using the Collection Helper Meteor package to add the feature: https://github.com/dburles/meteor-collection-helpers/



+1


source







All Articles