Question

i'm new with ember.js and i'm trying to make a websocket adapter to receive data in JSON from my server, my code maybe missing or incorrect .Ember chrome inspector gives this error: Cannot read property 'create' of undefined

this is my application.js:

   App = Ember.Application.create({
  LOG_TRANSITIONS: true

})

//Define a route for the template "post"
App.Router.map(function() {
  this.route("post", { path: "/post" });

});

App.PostRoute = Ember.Route.extend({
  model: function() {
    return this.store.find("post");
  }
});


//Post Model
App.Post = DS.Model.extend({
     name: DS.attr('string'),
     number: DS.attr('string')
});


App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.SocketAdapter.create({})
});

/************************* websocket Adapter ********************************************/

DS.SocketAdapter = DS.RESTAdapter.extend(App.WebSocketHandler, {
  socket: undefined,


 init: function(){
     this.socket = new App.WebSocketHandler('ws://localhost:8081/');
     this._super();
  },

  find: function (store, type, id) {
     // empty block
  },

  findAll: function (store, type) {
    // empty block
  },

  createRecord: function(store, type, record) {
    // code not relevant
  }
});

/**************************** websocket mixin ************************************/

App.WebSocketHandler = Ember.Object.extend({
  uri: 'ws://localhost:8081/',
  //ws: undefined

  initialize: function() {
   // this.ws = new WebSocket(this.uri);
    var ws = new WebSocket(uri);

    // callbacks
    this.ws.onopen = function() {
      console.log('Connection established /all');
    };
    this.ws.onclone = function() {
      console.log('Connection closed /' + 'all');
    };
    this.ws.onmessage = function(data) {
      DS.get('defaultStore').load(App.Post, data);  //Simply load your json in the store.
    };

    this._super();
  }
});

the problem is with :

   App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.SocketAdapter.create({})
});

but i couldn't resolve the problem!

Was it helpful?

Solution

you're defining DS.SocketAdapter after trying to use it. And you should be on Ember Data 1.0 beta 7 which defines adapters differently. You no longer define the store, just the adapters. Note that I defined DS.SocketAdapter, then I used it (in that order).

/********************* websocket Adapter ****************************************/

DS.SocketAdapter = DS.RESTAdapter.extend(App.WebSocketHandler, {
  socket: undefined,


  init: function(){
     this.socket = new App.WebSocketHandler('ws://localhost:8081/');
     this._super();
  },

  find: function (store, type, id) {
     // empty block
  },

  findAll: function (store, type) {
    // empty block
  },

  createRecord: function(store, type, record) {
    // code not relevant
  }
});

Defining the adapter

App.ApplicationAdapter = DS.SocketAdapter;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top