我有一个Storage
Ember对象,我注入route
和controller
。我可以访问Storage
中的route
,但不能访问controller
中的App.Storage = Ember.Object.extend
#...
#...
Ember.onLoad "Ember.Application", (Application)->
Application.initializer
name: "storage"
initialize: (container, application)->
application.register "storage:main", application.Storage, singleton: true
#Inject to both controller and route
application.inject "controller", "storage", "storage:main"
application.inject "route", "storage", "storage:main"
#I can access `Storage` here in Route
App.MessagesRoute = Ember.Route.extend
setupController: (controller, model)->
console.log @get('storage')
#OUTPUTS: Class {toString: function, __ember1397551513765: "ember299", __nextSuper: undefined, __ember1397551513765_meta: Object, constructor: function…}
#But I can't access `Storage` here in Controller
App.MessagesController = Ember.ArrayController.extend
actions:
createMessage: ->
console.log @get('storage')
#OUTPUTS: `undefined`
。 (Emberjs版本:1.5.1)
App.__container__.lookup('storage:main')
//OUTPUTS: Class {toString: function, __ember1397551513765: "ember299", __nextSuper: undefined, __ember1397551513765_meta: Object, constructor: function
App.__container__.lookup('route:messages').get('storage')
//OUTPUTS: Class {toString: function, __ember1397551513765: "ember299", __nextSuper: undefined, __ember1397551513765_meta: Object, constructor: function…}
App.__container__.lookup('controller:messages').get('storage')
//OUTPUTS: undefined
我还尝试在Chrome控制台上进行测试:
{{1}}
任何帮助都将不胜感激。