我有一个Message
模型,具有value
和author
属性。我可以在 Ember 代码(控制器、模型、视图等)中执行此操作:
this.store.push('message', msgObj)
但是,以下内容在全局范围内不起作用,例如将其放入<script src="websocket_processor.js">
:
msgObj = {value: 'Hello!', author: 'Jules'}
//I've tried the following but does not work
this.store.push('message', msgObj) //`this` doesn't point to ember
store.push('message', msgObj) //Console error: undefined store
App.Store.push('message', msgObj) //Uncaught TypeError: Object function () {if (!wasApplied) {...
我希望这是在余烬之外,因为我正在使用websocket-rails
gem 我使用以下函数
dispatcher.bind('add_message', function(data) { //add_message is just a method param from server
//Code where I need to use Ember to store say
this.store.push('message', data)
}
我现在被困了几个小时。任何帮助将不胜感激。谢谢!
可以使用应用程序初始值设定项在注册后执行作业并查找存储,以便可以在外部组件中使用它。
Ember.onLoad('Ember.Application', function (Application) {
Application.initializer({
name: "websocket-rails",
after: "store",
initialize: function (container, application) {
var store = container.lookup('store:main');
// Now you can inject store to component outside of Ember
}
});
});