设置控制器内容没有模型钩 [英] Set controllers content without model hook

查看:108
本文介绍了设置控制器内容没有模型钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行RC-3,并希望在没有模型钩子的情况下设置阵列控制器的内容。这是因为我需要添加一些过滤功能,并且不想在每次转换时重新加载内容。

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.

我发现 this.get 'content')有时是未定义的。我不知道为什么会这样。以下是代码:

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());
    }
  }
});

模型钩子的setupController中的等效代码是什么?

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

更新
我已经将其作为更完整的描述。

Update I've included this as a fuller description.

托管应用程序,并建立了。目前,我正在建立一个屏幕,用于行政/查看库存水平。 我正在尝试做的是一个屏幕,我可以在其上切换所有/特殊/ outofstock项目(根据todo,每个都有自己的路由),但是在屏幕上我需要过滤列表,例如按名称或标签。为了增加一个挑战,我根据过滤器(思想名称或标签)显示所有项目数量(全部为特殊的和缺货的),而不是在切换上(全部/特殊/缺货)

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; }
    });
  }
});

您会看到路由中唯一的区别是切换过滤器的定义,需要应用于模型(因为股票不同于股票/特殊或股票/ outofstock)

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.

想到任何人? / p>

Thoughts anybody?

推荐答案

您是否尝试使用某些数据种子筛选?

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.

这篇关于设置控制器内容没有模型钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆