简体   繁体   中英

RESTful API with Nodejs for Emberjs

I am attempting to implement a RESTful API server in Nodejs that exposes data to an Emberjs application. Currently I have created the front end and I believe I am able to communicate with the server, successfully requesting and receiving the data from the server but Ember is throwing me errors that I do not understand and more importantly cannot solve.

The code I have so far is as follows (It is in 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"

I would post the JS but it is messy. Anyway for the API I have fabricated some sample data that should be returned to the Emberjs application

#
# 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"

Looking at the network tab in the Chrome developers tools reveals that there was a successful response from the server and the data looks correct within the preview of the response. However the JS console is giving the following error:

Error while loading route: TypeError {stack: (...), message: "Cannot call method 'forEach' of undefined"}
Uncaught TypeError: Cannot call method 'forEach' of undefined 

Does anybody know what this could be?

Ember have your own way to extend classes.

You need to use:

App.SomeController = Ember.ObjectController.extend()

Instead of the coffee class declaration:

class App.SomeController extends Ember.ObjectController

So update your code to the following:

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"

I hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM