1
votes

SO,

I am working on an Ember app and experiencing a confusing problem. At the index route the app performs a find() and returns an array of

dataset
and links to a template to show further details about each
dataset
which are sideloaded when a resquest is made to find by id. (i.e. find(1), where 1 is the id.)

The first request with an id works fine, returning the dataset object and it's sideloaded data, however subsequent requests do not seem to do anything. The server does not see any request if I try to navigate to any other dataset after the first one's details have been loaded. However if I navigate from a specific dataset back to index and then back to any dataset it will send the request again (twice even, am not sure if this a related problem) and work. In other words:
/# works
/#/1 also works (or any other id as long as it is the first one visited)
/#/1 then /#/2 does not work, no request is sent
/#/1 followed by /# then /#/2 does work, maintaining the data at /#/1 & getting the new data for /#/2.

How do I get all of the specific dataset objects to return upon visiting them, without the hacky pitstop at index? Any advice would be greatly appreciated, thank you in advance!

The code:

-app.js


    /**************************
    * Application
    **************************/
    var App = Em.Application.create();

    App.Router.map(function() {
        this.resource('application', {path:'/'}, function() {
            this.resource('dataset', {path: '/:dataset_id'}, function() {

            });
        });
    });

    App.ApplicationRoute = Em.Route.extend({
        model: function() {
            return App.Dataset.find();
        }
    });

    App.DatasetRoute = Em.Route.extend({
        activate: function() {
            this.modelFor('dataset').reload();
        }
    });

    /**************************
    * Models
    **************************/
    App.Store = DS.Store.extend({
        adapter: DS.RESTAdapter.create({
            url: 'http://***.***.***.***:5000',
            namespace: 'api',
            serializer: DS.RESTSerializer.extend({
                primaryKey: function(type) {
                    return '_id';
                }
            })
        })
    });

    App.Dataset = DS.Model.extend({
        dataset: DS.attr('string'),
        title: DS.attr('string'),
        points: DS.hasMany('App.Point')
    });

    App.Point = DS.Model.extend({
        dataset: DS.attr('string'),
        dataset_id: DS.attr('string'),
        date: DS.attr('date'),
        value: DS.attr('string')
    });

1

1 Answers

3
votes

A route's activate hook is only called when the route is first transitioned to. It is not called again if the route's model changes. So when you transition into App.DatasetRoute either by entering the url directly or by clicking link on index page, the activate hook runs and your dataset is reloaded. When you switch from #/1 to #/2, the route remains active and no hook is called.

If I am understanding your question correctly, you want to reload the dataset whenever a user visits its url. In that case instead of the route's activate hook what you probably want to do is observe changes to the dataset controller's content. Something like this should work:

App.DatasetController = Ember.ObjectController.extend({
  refreshOnChange: function() {
    var dataset = this.get('content');
    if (dataset) {
      console.log('reloading dataset ', dataset.toString());
      dataset.reload();
    }
  }.observes('content')
}