我试图在Nodejs中实现RESTful API服务器,将数据公开给Emberjs应用程序。 目前我已经创建了前端,我相信我能够与服务器通信,成功地从服务器请求和接收数据,但是Ember给我带来了我不理解的错误,更重要的是无法解决。
我到目前为止的代码如下(它在coffeescript中):
window.Portal = Ember.Application.create
LOG_TRANSITIONS: true
LOG_TRANSITIONS_INTERNAL: true
rootElement: '#application'
class Portal.ApplicationSerializer extends DS.RESTSerializer
primaryKey: "_id"
class Portal.ApplicationAdapter extends DS.RESTAdapter
namespace: 'api/v1'
Portal.Router.map ->
@resource 'events', ->
@resource 'event', path: '/:event_id', ->
@route 'edit'
@route 'since_date', path: '/:since_date'
@route 'create'
@resource 'logs', ->
@resource 'log', path: '/:log_id', ->
@route 'comment'
@route 'since_date', path: '/:since_date'
@route 'dashboard', path: '/'
@route 'settings'
@route 'login'
@route 'logout'
@route 'missing', path: '/*path'
class Portal.LogsRoute extends Ember.Route
model: ->
@store.find 'log'
class Portal.Log extends DS.Model
name: DS.attr 'string'
created: DS.attr 'date'
modified: DS.attr 'date'
Portal.LogsController = Ember.ArrayController.extend
sortProperties: ['created']
sortAscending: true
logsCount: (->
@get "model.length"
).property "@each"
我会发布JS,但它很混乱。 无论如何,我已经制作了一些应该返回给Emberjs应用程序的样本数据
#
# Logs Routes
#
module.exports = (app) ->
app.namespace '/v1', ->
app.get '/logs', (req, res) ->
res.json "logs": [ { "_id": 1, "name": "name1", "created": "2013-02-07T16:44:57.000Z", "modified": "2013-02-07T16:44:57.000Z" }, { "_id": 2, "name": "name2", "created": "2013-02-07T16:44:57.000Z", "modified": "2013-02-07T16:44:57.000Z" } ]
app.get '/logs/:id', (req, res) ->
res.json "log": { "_id": 1, "name": "name2", "created": "2013-02-07T16:44:57.000Z", "modified": "2013-02-07T16:44:57.000Z" }
app.post '/logs/:id/comment', (req, res) ->
console.log "posted"
app.delete '/logs/:id', (req, res) ->
console.log "removed"
app.delete '/logs', (req, res) ->
console.log "removed"
查看Chrome开发人员工具中的网络选项卡,可以看出服务器响应成功,数据在预览响应中看起来是正确的。 但是JS控制台出现以下错误:
Error while loading route: TypeError {stack: (...), message: "Cannot call method 'forEach' of undefined"}
Uncaught TypeError: Cannot call method 'forEach' of undefined
有谁知道这可能是什么?
Ember有自己的方法来扩展课程。
你需要使用:
App.SomeController = Ember.ObjectController.extend()
而不是咖啡类声明:
class App.SomeController extends Ember.ObjectController
因此,请将代码更新为以下内容:
window.Portal = Ember.Application.create
LOG_TRANSITIONS: true
LOG_TRANSITIONS_INTERNAL: true
rootElement: '#application'
Portal.ApplicationSerializer = DS.RESTSerializer.extend
primaryKey: "_id"
Portal.ApplicationAdapter = DS.RESTAdapter.extend
namespace: 'api/v1'
Portal.Router.map ->
@resource 'events', ->
@resource 'event', path: '/:event_id', ->
@route 'edit'
@route 'since_date', path: '/:since_date'
@route 'create'
@resource 'logs', ->
@resource 'log', path: '/:log_id', ->
@route 'comment'
@route 'since_date', path: '/:since_date'
@route 'dashboard', path: '/'
@route 'settings'
@route 'login'
@route 'logout'
@route 'missing', path: '/*path'
Portal.LogsRoute = Ember.Route.extend
model: ->
@store.find 'log'
Portal.Log = DS.Model.extend
name: DS.attr 'string'
created: DS.attr 'date'
modified: DS.attr 'date'
Portal.LogsController = Ember.ArrayController.extend
sortProperties: ['created']
sortAscending: true
logsCount: (->
@get "model.length"
).property "@each"
我希望它有所帮助