Model that depends on the current state


#1

I have a REST API that allows me to filter entities. I want to provide a quick access to those filtered entities from within my ember.js application. For example, routes /entities/my and /entities/group.

The question is how to access these “my” and “group” from within my model? Should I use transition parameter or is there a more elegant way? I also need to access the ApplicationController from within my model.

App.Router.map(function () {
  this.resource("application");
  this.resource("entities", { path: "/entities" }, function(){
    this.route("my");
    this.route("group");
  });  
});

App.EntitiesRoute = Em.Route.extend({
  model: function (params, transition) {
    console.log(transition.targetName);
    // I also need to access the ApplicationController
    // from within my model
  }
});

Another related question is where should I define those AJAX requests? Should they go to the model or to the controller?

Is there an example on how do I fill in model from within my controller?


#2

Ok, I end up with something like this:

App.Router.map(function () {
  this.resource("application");
  this.resource("entities", { path: "/entities" }, function(){
    this.route("my");
    this.route("group");
  });  
});

App.EntitiesMyRoute = Em.Route.extend({
  setupController: function (controller, model) {
    controller.fetch();
  }
});

App.EntitiesGroupRoute = Em.Route.extend({
  setupController: function (controller, model) {
    controller.fetch();
  }
});

App.EntitiesController = Ember.ArrayController.extend({
});
App.EntitiesMyController = Ember.ArrayController.extend({
  needs: ['entities', 'application'],
  fetch: function() {
    console.log(this.get('controllers.application.currentUser'));
    this.set('controllers.entities.model', App.Entity.find(/* custom params */));
  }
});
App.EntitiesTeamController = Ember.ArrayController.extend({
  needs: 'entities',
  fetch: function() {
    this.set('controllers.entities.model', App.Entity.find(/* custom params */));
  }
});

Is there any better solution?

The question remains how to define this custom Entity model? I mean is there a tutorial on how to create highly customized models?