ember无法访问websocketAdapter

时间:2014-05-09 10:51:20

标签: ember.js websocket

我是ember的新手,我正在尝试做一个实时从我的golang Server接收JSON数据的websocket,我的代码的问题是它似乎没有读取我拥有的适配器创建,所以我的套接字不连接到服务器。我认为我的代码在application.js或index.html中遗漏了一些东西,这将允许我访问我的适配器并使用它的方法! 这是我的application.js:

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

})
/******************************* Post Template **************************************/

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

});

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

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


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

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

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


 init: function(){

     this.socket = new App.WebSocketHandler();
     this._super();
  },

  find: function (store, type, id) {
     // empty block
      console.log('find');
  },

  findAll: function (store, type) {
    // empty block
     console.log('findAll');
  },

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



App.ApplicationAdapter = DS.SocketAdapter;

// Use the adapter in the store

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

和我的index.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <title>Ember.js Example Application</title>

   <script src="js/libs/jquery-1.10.2.js"></script>
   <script src="js/libs/handlebars-1.1.2.js"></script>
   <script src="js/libs/ember-1.5.1.js"></script>
   <script src="js/libs/Ember_Data.js"></script>
   <script src="js/application.js"></script>
   <script src="js/router.js"></script>
   <script src="js/models/model.js"></script>

</head>
<body>
<h1>Bonjour </h1>


<script type="text/x-handlebars">
    Hello, {{firstName}} {{lastName}}<br/>

    <nav>
  {{#link-to 'post'}}Post{{/link-to}}
  </nav>

     <div>
      {{outlet}}
    </div>

</script>


<script type="text/x-handlebars" data-template-name="index">
    <h2>My Wrappers</h2>
    <ul>
    {{#each post in model}}
        <li>{{post.number}}</li>

    {{/each}}
    </ul>
  </script></p>


  <script type="text/x-handlebars" data-template-name="post">
    <ul>
        {{#each post in model}}
            <li>Wrapper Name: {{post.name}}</li>
            <li>Wrapper Number: {{post.number}}</li>
        {{/each}}
    </ul>
  </script>


<script type="text/javascript">

</script>


</head>
<body>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些错误。

1)Ember Object使用自己的创建方法。

this.socket = new App.WebSocketHandler();
// change to
this.socket = App.WebSocketHandler.create({});

2)您的路线和路线模板未正确命名为帖子

this.route(&#34;帖子&#34;,{路径:&#34; / posts&#34;});

http://emberjs.jsbin.com/qixur/1/edit