2014-03-25 32 views
0

我有幾個(簡單)模型,如語言,部門等,它們只包含名稱和id屬性(列)。我想製作一個控制器和視圖,它控制着CRUD功能。我應該如何處理這個問題,讓幾個模型有一個控制器?ember.js一個控制器/查看更多模型

是否可以從路由變量加載模型?

僞代碼

somecontroller/MODELNAME

App.IndexRoute = Ember.Route.extend({ 
    model: function(modelname) { 
     return this.get('store').find(modelname); 
    } 
}); 

回答

2

可以從模型鉤加載多個模型,並將它們分配到控制器的性能。例如

App.IndexRoute = Ember.Route.extend({ 
    model: function(modelname) { 
    var store = this.get('store'); 
    return Ember.RSVP.hash({ 
     foos: store.find('foos'), 
     bars: store.find('bars') 
    }); 
    }, 
    setupController: function(controller, model) { 
    controller.set('foos', model.foos); 
    controller.set('bars', model.bars); 
    } 
}); 

Ember.RSVP.hash將返回上傳遞的對象的所有屬性的承諾值等待一個承諾,然後將用相同的屬性名和還願結果值的對象履行。

通過覆蓋setupController,您可以確定在控制器上設置了哪些屬性以及哪些值。

0

這裏有兩種方式,你可以做到這一點得到一個路線

/* 
    * Use the model hook to return model, then 
    * setController to set another model 
*/ 
App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.findAll('languages'); 
    }, 
    setupController: function(controller, model) { 
    this._super(controller, model); 
    controller.set('department', this.store.findAll('department')); 
    } 
}); 


/* 
    * Can return a Hash of promises from the model hook 
    * and then use those as your models 
    */ 
App.RsvphashRoute = Ember.Route.extend({ 
    model: function() { 
    return Ember.RSVP.hash({ 
     langs: this.store.find('languages1'), 
     dept: this.store.find('department1')  
    }); 
    } 
}); 

兩個模型這裏是他們行動的jsbin。希望它有幫助:

http://emberjs.jsbin.com/basoneno/1/edit

相關問題