带有Ember和Ember-Data的Socket.IO

时间:2013-03-13 04:03:22

标签: ember.js socket.io ember-data

我一直在四处寻找,我找不到任何最新的ember(1.0.0-rc.1)和ember-data(版本11)也使用socket.io的例子。我尝试过这样的事情。

App.ApplicationRoute = Ember.Route.extend({
  setupController: function(controller, data) {
    var socket = io.connect(),
        self = this;
    socket.on('apartment/new', function(apartment) {
      var apt = App.Apartment.createRecord(apartment);
      self.controllerFor('apartments').pushObject(apt);
    });
  }
});

这实际上会创建一个新的模型类,它将对象推送到控制器,并创建一个新的li但值不会呈现。

<ul class="list-view">
{{#each apartment in controller}}
    <li>
      {{#linkTo 'apartment' apartment }}
        <span class="date">{{date apartment.date}}</span>
        {{apartment.title}}
      {{/linkTo}}
    </li>
{{/each}}
</ul>

这是否与运行循环有关?如何强制渲染值?或者有更好的方法吗?

2 个答案:

答案 0 :(得分:15)

这是一个非常简单的解决方案,我在我的一些应用程序中使用它。您可以为套接字设置通用回调并接受任何类型的数据

callback: function(message) {
  // this is better than just `eval`
  var type = Ember.get(Ember.lookup, message.type);
  store.load(type, message.data);
}

或此处它专门针对您的用例而定制

socket.on('apartment/new', function(apartment) {
  store.load(App.Apartment, apartment);
});

使用store.load会将记录数据直接加载到身份映射中。还有loadMany用于加载多个记录。

答案 1 :(得分:1)

Ember CLIember socketscoffescript

我只与Ember合作了大约一个月,所以没有承诺这是最好的方法,但是我不得不用Socket.io建立一个Ember CLI项目。 Ember数据。以下是基于我的工作代码,但未对以下内容进行测试。我认为你需要99%的所有东西都在这里。祝你好运排除故障!

.jshintrc - 如果你不在这里包括EmberSockets,Ember服务会对你大喊

{
  "predef": {
  "document": true,
  "window": true,
  "nameofprojectENV": true,
  "EmberSockets": true
  },
  ...
}

Brocfile.js - 导入Ember套接字

var EmberApp = require('ember-cli/lib/broccoli/ember-app');

var app = new EmberApp();

// change this path to where ember-sockets.js is
app.import('vendor/ember-sockets/package/ember-sockets.js');

module.exports = app.toTree();

app.js - 记得将nameofproject替换为实际项目名称

import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';

Ember.MODEL_FACTORY_INJECTIONS = true;

// every controller using ember sockets must be listed here in the controllers array
var Socket = EmberSockets.extend({
    host: 'example.local',
    port: 8080,
    controllers: [
        'example'
        // more controllers here
    ]
});

var App = Ember.Application.extend({
    modulePrefix: 'nameofproject',
    Resolver: Resolver,
    Socket: Socket
});

loadInitializers(App, 'nameofproject');

export default App;

router.coffee - 记得将nameofproject替换为实际项目名称

`import Ember from 'ember'`

Router = Ember.Router.extend
    location: nameofprojectENV.locationType

Router.map ->
    @route 'example'

`export default Router`

模型/ example.coffee

`import DS from 'ember-data'`

Example = DS.Model.extend
    name: DS.attr('string')

`export default Example`

路由/ example.coffee

`import Ember from 'ember'`

ExampleRoute = Ember.Route.extend
    # set model to be all example records
    model: ->
        @store.all('example')

    setupController: (controller, model) ->
        controller.set('model', model)

`export default ExampleRoute`

控制器/ example.coffee

`import Ember from 'ember'`

ExampleController = Ember.Controller.extend

#### properties

examples: (->
    # @get('content') gets the model, which in this case, is example (set in route)
    @get('content')
).property('content') # watching the model like this might not be right?

#### methods
getExamples: ->
    @socket.emit 'pub',
        # whatever data you need to pass to the server
        data : {examples: true}
        # name of the event you want the data returned on
        event: "getExamples"

#### sockets
sockets:
    # returns examples from server
    getExamples: (data) ->
        # log the data for fun, also to see that you are getting data back
        console.log data
        # set controller to @, which is the same as this.
        controller = @

        # get your array of examples from JSON returned from server
        examples = data.examples

        examples.forEach (example) ->
            # controller instead of @, or it doesn't work
            controller.store.push 'example',
                # you need ids or this will not work
                id: example.id
                name: example.name

`export default ExampleController`

模板/ example.coffee

{{#each example in examples}}
    {{example.id}} {{example.name}}
{{/each}}

<button {{action getExamples}}>Get Examples</button>

我建议Chrome Ember Inspector进行问题排查。除了其他方面,您还可以查看数据模型和记录。