, , /.
App.ApplicationRoute = Ember.Route.extend({
beforeModel: function(){
var service = this.controllerFor('service');
}
});
App.ServiceController = Em.Controller.extend({
init: function(){
this._super();
this.startFooConsole();
},
startFooConsole: function(){
Em.run.later(this, this.startFooConsole, 1000);
console.log('hello world');
},
helloWorld: function(){
console.log('hello world function');
}
});
this.controllerFor('service').helloWorld();
App.FooController = Em.Controller.extend({
needs:['service'],
someMethod: function(){
this.get('controllers.service').helloWorld();
}
})
http://emberjs.jsbin.com/bukuvuho/1/edit
(Ember Dependency Injection)
, , ( ).
App.Service = Em.Object.extend({
init: function(){
this._super();
this.startFooConsole();
},
startFooConsole: function(){
Em.run.later(this, this.startFooConsole, 1000);
console.log('hello world');
},
helloWorld: function(){
console.log('hello world function');
}
});
App.initializer({
name: "service",
initialize: function (container, application) {
var service = application.Service.create();
application.register("my:service", service, {instantiate:false});
application.inject("controller", "service", "my:service");
application.inject("route", "service", "my:service");
application.service = service;
}
});
http://emberjs.jsbin.com/bukuvuho/2/edit