ember websocket适配器:无法读取属性'创建'未定义的

时间:2014-05-08 16:50:49

标签: ember.js websocket adapter

我是ember.js的新手,我试图让websocket适配器从我的服务器接收JSON中的数据,我的代码可能丢失或不正确.Ember chrome inspector给出了这个错误:无法读取财产'创造'未定义的

这是我的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();
  }
});

问题在于:

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

但我无法解决问题!

1 个答案:

答案 0 :(得分:0)

您在尝试使用DS.SocketAdapter后正在定义它。你应该使用Ember Data 1.0 beta 7,它以不同的方式定义适配器。您不再定义商店,只定义适配器。请注意,我定义了DS.SocketAdapter,然后我使用它(按此顺序)。

/ ********************* 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
  }
});

定义适配器

App.ApplicationAdapter = DS.SocketAdapter;