我使用 EmberJS 并尝试使用 websocket (socket.io) 从后端获取数据,所以我设置了这个应用程序路由
App.ApplicationRoute = Ember.Route.extend(
setupController: (controller, data) ->
store = @get 'store'
socket = io.connect "http://localhost:4000/orders" ## Line 4
socket.on "new_order", (order) ->
store.load(App.Order, order)
socket.on "new_billing", (bill) ->
store.load(App.Bill, bill)
socket.on "connected", ->
console.log "Ready"
model: ->
return { title: "Ordenes" }
actions:
markAsDone: (type, type_id) ->
# Send value to backend
socket.emit "confirm_" + type, ## Line 16
id: type_id
# Find record by id
if type == "order"
record = App.Order.find(type_id)
transition = "orders"
else if type == "bill"
record = App.Bill.find(type_id)
transition = "bills"
# Delete from store
record.then( (r) ->
r.deleteRecord()
)
# Display list of record type
@transitionTo(transition)
)
在第 4 行,当我点击“/”时,正在设置连接并获取对象,但是在我输入路线后,“/orders”对象不再被获取,在上面代码的第 16 行,我无法使用套接字变量
Uncaught ReferenceError: socket is not defined
有没有更好的方法来管理这个?