2013-06-05 31 views
1

我正在運行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條路徑上工作的過濾器,我認爲這個部分使得這個過程相當完善。

想到有人嗎?

+0

我不確定爲什麼不使用模型鉤子,返回一個過濾器。這可能與你的其他問題有關? –

+0

是的,過濾器是可變的。我在3個控制器中使用相同的模型,但每個控制器代表一個不同的過濾器(他們在那裏,以便我可以通過URL訪問過濾器)。然後每個頁面都有附加的過濾器,這些過濾器在3個控制器中保留。有一個模型掛鉤重置所有的過濾器。 – Gevious

+0

我很想看到有關控制器的更多代碼。如果他們都依靠自己的路線,我認爲你可以有三個模型掛鉤。 –

回答

0

您是否嘗試用某些數據爲您的過濾器播種?

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

這應該從商店獲取物化模型,並阻止對服務器進行任何更多的調用。

+0

過濾器是動態的,它需要控制器實例來填充它。因此有關在setupController中設置它的問題。如果有一種方法可以將這些信息加入到模型鉤子中,那麼我就是耳朵。 – Gevious