Pregunta

I have two separate views:

  1. List of posts
  2. List of comments connected with particular post

When I click on particular post in #1 view I'm displaying #2 view using simple transitionTo('comments', post). What I want to do is to mark all comments connected with the post as read when they are displayed. Let's say I want to run method: markAllAsRead(comment). Where I should put this business logic?

The ideal would be to add some hook to controller on loading. Can't find anything like this, init method is called only on first load of #2 view.

I can also run this in router in setupController

App.IndexRoute = Ember.Route.extend({ 
  setupControler: function(controller, model){
    this._super(controller, model);
    markAllAsRead(model);
  }
});

But it router doesn't seems to be designed to keep such logic.

¿Fue útil?

Solución

You could always add your own initialize function to your controller, and in your route's setupController function controller.initializeComments(model), for example, and perform your necessary setup from the controller every time the route is loaded.

Otros consejos

Please always post a jsbin/jsfiddle with an example. It makes understanding your structure and thus problem much easier.

You should have two Routes:

  • App.PostsRoute
  • App.CommentsRoute

The Router could look something like this:

App.Router.map(function() {
  this.resource('posts', function () {
    this.resource('comments', { path: '/posts/:post_id/comments' });
  });
});

In the model hook of the App.PostsController and App.CommentsController you have to load the data. You can then use the afterModel hook on the App.CommentsController to manipulate the properties of the passed models.

App.CommentsRoute = Ember.Route.extend({
  afterModel: function(comments, transition) {
    comments.setEach('read', true); 
    // sets the property 'read' of all elements of the comments array to true
  }
});

Documentation:

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top