我正在运行RC-3,希望在没有模型挂钩的情况下设置数组控制器的内容。这是因为我需要添加一些过滤,并且不希望每次转换都重新加载内容。
我发现this.get('content')有时是没有定义的。我不知道这是为什么。下面是代码:
App.StockRoute = Em.Route.extend({
setupController: function(controller) {
if (controller.get('content') === undefined) {
controller.set('content', App.Stock.find());
}
}
});模型钩子的setupController中的等效代码是什么?
更新--我已经把它作为一个更全面的描述。
我采用了成员指南的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; }
});
}
});您将看到路由的唯一区别是切换过滤器的定义,需要将其应用于模型(因为股票不同于股票/特殊或股票/外挂股票)
我还没有弄清楚如何将一个控制器连接到多个路由,因此在控制器端有以下内容
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({});给你拿着。这相当复杂,也许是因为我不太清楚如何在ember中正确地做到这一点。事实上,我有一个基于url的切换和一个在这3条路径上工作的过滤器,我认为,这部分使这相当复杂。
有人在想吗?
发布于 2013-06-06 13:37:15
你试过用一些数据为你的过滤器添加种子吗?
App.Stock.filter { page: 1 }, (data) -> data这将从存储中获取物化模型,并防止对服务器进行更多的调用。
https://stackoverflow.com/questions/16936956
复制相似问题