我現在正在試驗在燼路由,並有一個工作的例子。問題是,我有點困惑爲什麼它有效。目前這條路線只有2個簡單的意見。下面是代碼:誰在爲我實例化我的對象? :s
App = Em.Application.create();
App.Router = Ember.Router.extend({
root: Ember.Route.extend({
index: Ember.Route.extend({
route: '/',
redirectsTo: 'home' //when hitting the base URL, redirect to home
}),
home: Ember.Route.extend({
route: '/home',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('home');
}
}),
about: Ember.Route.extend({
route: '/about',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('about');
}
})
})
});
//Main controller + view
App.ApplicationController = Ember.Controller.extend({});
App.ApplicationView = Ember.View.extend({
templateName: 'application',
goHome: function(){
App.router.transitionTo('home');
},
goAbout: function(){
App.router.transitionTo('about');
}
});
// Home page
App.HomeView = Ember.View.extend({
templateName: 'home'
})
// About page
App.AboutController = Ember.Controller.extend({
numWidgets: 45
})
App.AboutView = Ember.View.extend({
numWidgetsBinding: 'App.aboutController.numWidgets',
templateName: 'about'
})
App.initialize();
在我的HTML我只是有幾個非常簡單的模板用的名字「應用程序」,「家」和「約」。
所以,它的一切工作,看起來非常類似於網上浮動的所有例子。大!但我很困惑,看起來我有幾件事爲我實例化,沒有我要求去做。它是否正確?
例如:
它是如何產生的ApplicationController的實例?
在connectOutlets函數中,它正在尋找一個名爲「applicationController」的控制器。我從來沒有創建任何名爲「applicationController」(小寫「a」),我只是擴展控制器,並稱它爲「ApplicationController」(大寫「A」)。爲什麼這個工作?
它是如何創建一個AboutController的實例?
我做了一個簡單的「約」頁面視圖和控制器之間的測試綁定。在視圖中,我綁定了變量'App.aboutController.numWidgets'。我從來沒有調用過App.AboutController.create()。那麼如何爲我準備好這樣的一個實例?再次,它有一個小寫字母(「aboutController」)。所有我做過的是延長控制器(和大寫字母將其命名爲 - 「AboutController」)
一點解釋將是巨大的,因爲像任何正常的開發者,我覺得用在這裏你不代碼知道爲什麼它的工作很瘋狂!