我正在运行RC-3并且想要在没有模型挂钩的情况下设置arraycontroller的内容。这是因为我需要添加一些过滤功能,并且不想在每次转换时重新加载内容。设置控制器内容没有模型挂钩
我发现this.get('content')
有时是不确定的。我不确定这是为什么。这里的代码:
App.StockRoute = Em.Route.extend({
setupController: function(controller) {
if (controller.get('content') === undefined) {
controller.set('content', App.Stock.find());
}
}
});
什么是在setupController模型钩子的等效代码?
更新 我已经将此作为一个更全面的描述。
我拿了todo应用程序的余烬指南,并建立了它。目前我正在构建一个屏幕来查看/查看库存水平。 我想要做什么有一个屏幕上我可以切换所有/特价/ outofstock项目(按todo,每个都有自己的路线),但然后在屏幕上,我需要过滤列表,例如通过名称或标签。要增加一个挑战,我会根据筛选器(想想名称或标签)而不是切换(在全部/特殊/特殊情况下),在屏幕上始终显示所有项目的数量(全部,特殊和缺货)出
自基本上是一个屏幕的股票),我做的航线代码
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)
我还没有想出如何链接一个控制器到多个路由,所以我有以下在控制器端
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({});
你有它。它相当复杂,也许是因为我不确定如何在余烬中正确执行此操作。事实上,我有一个基于URL的切换和一个可以在这3条路径上工作的过滤器,我认为这个部分使得这个过程相当完善。
想到有人吗?
我不确定为什么不使用模型钩子,返回一个过滤器。这可能与你的其他问题有关? –
是的,过滤器是可变的。我在3个控制器中使用相同的模型,但每个控制器代表一个不同的过滤器(他们在那里,以便我可以通过URL访问过滤器)。然后每个页面都有附加的过滤器,这些过滤器在3个控制器中保留。有一个模型挂钩重置所有的过滤器。 – Gevious
我很想看到有关控制器的更多代码。如果他们都依靠自己的路线,我认为你可以有三个模型挂钩。 –