2015-10-13 51 views
0

我想操縱從store對象(this.store.find('...')等)獲得的數據,如限制(我不能只向API發送限制,因爲我只是使用Fixtures此時)或扭轉結果。操作來自商店的數據

對於限制我已經嘗試實施limit幫手,並在each塊(#each (limit recentItems 5) ...)中調用它;

import Ember from 'ember'; 

export default Ember.Helper.helper(function(params) { 
    return params[0].slice(0, params[1]); 
}); 

我不知道爲什麼它沒有工作,但它似乎結果對象不是一個數組,因此我無法切片。恢復也是一樣。

謝謝。

+0

您可以發佈store.find的結果呢? –

+0

@kristjanreinhold我有點困惑。結果只是一個對象 - 你想看到什麼?有趣的是,評論消失了,但是,我確實看到了http://stackoverflow.com/questions/18077649/emberjs-data-limit-collections,但是我有多個模型 - 因此this.get('content')'doesn'回報我需要的東西。 – nehalist

+0

http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html嘗試findAll('xx/xx')它是否仍然返回一個對象。結果肯定應該是n數組。你確定你沒有使用find('x/y',1)? –

回答

0

操作模型正是您的路線上的afterModel方法的用途。

它得到了你解決的模型(所以你不必擔心承諾),你可以修改它的內容。

App.PostsRoute = Ember.Route.extend({ 
    afterModel: function(model, transition) { 
    // Modify the model here 
    } 
}); 

afterModel documentation

編輯:在評論中討論後附加的建議...

如果你想獲取額外的模型(模型鉤以外),並具有可訪問在控制器上,然後你可以在你的路徑上的setupController鉤子中獲取它,並在將它設置在控制器上之前修改它。

setupController: function(controller, model) { 

    // Default functionality 
    this._super(controller, model); 

    // Create items ArrayProxy and set it on the controller 
    var arr = Ember.ArrayProxy.create({}); 
    controller.set('recentItems', arr); 

    // Fetch additional model 
    this.store.find('items').then(function(items) { 

    // Promise has resolved by this point 

    var i = 0; 

    // Iterate over first 5 returned items 

    while (i < 5) { 

     // Add each item to recentItems property on controller 
     controller.get('recentItems').addObject(items[i]); 

    } 

    }); 

} 

setupController documentation

+0

我想我可能會使用錯誤的措辭。我不想直接操作模型,但它的實例。例如:我使用'store.find('category',category_id)'獲得所有類別的項目。 'category.items'現在擁有我所有的物品,但我只想顯示5個物品,或者反轉它們,或者其他什麼。基本上我想操縱每個循環數據。 – nehalist

+0

好的,這些數據是從您的路線上的模型鉤子中獲取的?或者你是否從別處調用store.find()? – joshfarrant

+0

當前路由的模型在路由器中設置。但我需要一個額外的模型(最後的項目),它在控制器中設置(通過'recentItems:function(){return this.store.find('items');}')。 – nehalist