1
votes

I have a route, and the data that want to return for that route needs to come from an AJAX REST request to a server, the response of this request should be used as the template data so I can loop over the results in the template.

What is the easiest way to achieve this? I'd need this to either be a synchronous call inside my data function that get's passed into the template. Like this:

Router.map(function() { 

this.route('test', {
    path: '/test',
    data: function() {

        var templateData = { title : 'My Redeemed Rewards', menu : 'ion-navicon', parent : 'myRewards' }

        // Make syncronous request and extend the template data object with the json response

        return templateData;

    }
 });

});

Or have it be an asynchronous request and have the template re-render when the data is available. How would i get the template to redraw in this case?

2
I dont know about the framework you are using but this is usually done by databinding (e.g. angularJS) your app holds a controller which your view is assigned to. Then if you change any of the data in the controller it will also be updated in the view. Maybe try to think about going with AngularJS if you have the possibility - I think its cutting edge for this purpose - Max Bumaye

2 Answers

0
votes

You can create in memory collection by passing null as name. http://docs.meteor.com/#/full/collections
new Mongo.Collection(name, [options])

name String The name of the collection. If null, creates an unmanaged (unsynchronized) local collection.

Than create method that can be called by clients. http://docs.meteor.com/#/full/meteor_methods
The method will load the data via REST from other server and puts it into the local collection.

0
votes

Check this article: http://www.manuel-schoebel.com/blog/use-meteor-iron-router-waiton-to-load-external-javascript

I'm not sure it works with Meteor 1.0 but It won't be hard to recode it.