2013-03-20 24 views
1

我的路由結構:Ember.js RC1 - 控制器「需求」另一個尚不存在

App.ready = function() { 
    App.Router.map(function() { 
     this.resource('contacts', function() { 
      this.resource('contact', function() { 
      }); 
     }); 
    }); 
} 

現在在我的contactsController我回應並add的行動,過渡到接觸路線。然後我想在我的contactController上撥打add方法。

我已經放在needs: ['contact']ContactController但後來我得到這個消息:

<App.ContactsController:ember197> needs controller:contact but it does not exist 

當我使用controllerFor(其中不建議使用)我還得到一個錯誤:

this.controllerFor('contact').add(); 

因此,一旦實際轉換到適當的路由,Ember.js RC1似乎只創建控制器(和其他相關實例)。

有沒有辦法解決這個問題。

回答

2

So Ember.js RC1 appears to only create the controllers (and other related instances) once one actually transitions to the appropriate route.

有趣的 - 我以前認爲燼生成的控制器,但不要猜測。

Is there a way around this?

解決方法是手動定義App.ContactController。像這樣將工作:

App = Ember.Application.create({}); 

App.Router.map(function() { 
    this.resource('contacts', function() { 
     this.resource('contact', function() { 
     }); 
    }); 
}); 

App.ContactController = Ember.Controller.extend({ 
    add: function() { 
    alert('App.ContactController.add() was called!'); 
    } 
}); 

App.ContactsController = Ember.Controller.extend({ 
    needs: ['contact'], 
    add: function() { 
    this.get('controllers.contact').add(); 
    } 
}); 

http://jsbin.com/osapal/1/edit

+0

OK,我看到我做錯了什麼。對於聯繫人,我有一個「ContactIndexController」,因爲我也在玩聯繫下嵌套的資源。在這種情況下,我需要'需要:['contactIndex']'。我還在'ContactController'中有'needs:['contacts']',所以控制器在處理'needs'時似乎不需要實例化。命名約定與'Index'後綴有點混淆,但不是火車粉碎。一旦我更好地掌握了Ember,它可能會有意義。 – 2013-03-20 08:21:56

相關問題