1
votes

I'm running RC-3 and want to setup the content of an arraycontroller without the model hook. This is because I need to add some filtering and don't want to reload the content with every transition.

I found that this.get('content') is sometimes undefined. I'm not sure why this is. Here's the code:

App.StockRoute = Em.Route.extend({
  setupController: function(controller) {
    if (controller.get('content') === undefined) {
      controller.set('content', App.Stock.find());
    }
  }
});

What is the equivalent code in the setupController for the model hook?

Update I've included this as a fuller description.

I took the ember guide of the todo app, and built off that. Currently I'm building a screen to mangage/view stock levels. What I'm trying to do is have a screen on which I can toggle all/specials/outofstock items (as per the todo, each has its own route), but then on the screen I need to filter the list eg by name or by tag. To add a challenge, I display the number of items (all, on special and out of stock) on the screen all the time, based on the filter (think name or tag) but not on the toggle (think all/on special/ out of stock)

Since its essentially one screen, I've done the following in the route code

App.StockIndexRoute = Em.Route.extend({
  model: function() {
    return App.Stock.find();
  },
  setupController: function(controller) {
//    if (controller.get('content') === undefined) {
//      controller.set('content', App.Stock.find());
//    }
    // sync category filter from object outside controller (to match the 3 controllers)
    if (controller.get('category') != App.StockFilter.get('category')) {
      controller.set('category', App.StockFilter.get('category'));
      controller.set('categoryFilter', App.StockFilter.get('category'));
    }
    // a hack so that I can have the relevant toggle filter in the controller
    if (controller.toString().indexOf('StockIndexController') > 0) {
      controller.set('toggleFilter', function(stock) { return true; });
    }
  }
});

App.StockSpecialsRoute = App.StockIndexRoute.extend({
  setupController: function(controller) {
    this._super(controller);
    controller.set('toggleFilter', function(stock) {
      if (stock.get('onSpecial')) { return true; }
    });
  }
});

App.StockOutofstockRoute = App.StockIndexRoute.extend({
  setupController: function(controller) {
    this._super(controller);
    controller.set('toggleFilter', function(stock) {
      if (stock.get('quantity') === 0) { return true; }
    });
  }
});

You'll see that the only difference in the routes is the definition of the toggle filter, which needs to be applied to the model (since stock is different to stock/special or to stock/outofstock)

I haven't yet figured out how to link one controller to multiple routes, so I have the following on the controller side

App.StockIndexController = Em.ArrayController.extend({
  categoryFilter: undefined,
  specialCount: function() {
    return this.get('content').filterProperty('onSpecial', true).get('length');
  }.property('@each.onSpecial'),
  outofstockCount: function() {
    return this.get('content').filterProperty('quantity', 0).get('length');
  }.property('@each.quantity'),
  totalCount: function() {
    return this.get('content').get('length');
  }.property('@each'),
  // this is a content proxy which holds the items displayed. We need this, since the 
  // numbering calculated above is based on all filtered tiems before toggles are added
  items: function() {
    Em.debug("Updating items based on toggled state");
    var items = this.get('content');
    if (this.get('toggleFilter') !== undefined) {
      items = this.get('content').filter(this.get('toggleFilter'));
    }
    return items;
  }.property('toggleFilter', '@each'),
  updateContent: function() {
    Em.debug("Updating content based on category filter");
    if (this.get('content').get('length') < 1) {
      return;
    }
    //TODO add filter
    this.set('content', content);
    // wrap this in a then to make sure data is loaded
    Em.debug("Got all categories, lets filter the items");
  }.observes('categoryFilter'),
  setCategoryFilter: function() {
    this.set('categoryFilter', this.get('category'));
    App.StockFilter.set('category', this.get('category'));
  }
});

// notice both these controllers inherit the above controller exactly
App.StockSpecialsController = App.StockIndexController.extend({});
App.StockOutofstockController = App.StockIndexController.extend({});

There you have it. Its rather complicated, perhaps because I'm not exactly sure how to do this properly in ember. The fact that I have one url based toggle and a filter that works across those 3 routes is, I think, the part that makes this quite compicated.

Thoughts anybody?

1
I'm not sure why not using the model hook, returning a filter. This is probably related to your other question ? - sly7_7
Yes, the filter is variable. I use the same model in 3 controllers, but each controller represents a different filter (They're there so that i can access the filter via the url). Then each page has additional filters which persist across the 3 controllers. Having a model hook resets all the filters. - Gevious
I'm curious to see little more code, concerning the controllers. If they all relies on their own routes, I think you could have three model hooks. - sly7_7

1 Answers

0
votes

Have you tried to seed your filter with some data?

App.Stock.filter { page: 1 }, (data) -> data

That should grab the materialized models from the store, and prevent making any more calls to the server.