【问题标题】:How do you load data into Ember Data from a Socket.IO response?如何从 Socket.IO 响应中将数据加载到 Ember Data 中?
【发布时间】:2013-02-26 15:17:51
【问题描述】:

我正在编写一个利用 Ember.js、Ember Data 和通过 Node.js 的 Socket.IO 的应用程序。我正在尝试找出从套接字响应接收数据并将其加载到我的 Ember 数据存储中的最佳方法,例如:

window.socket = io.connect('http://localhost');

socket.on('action here', function(response) {
  // how to load "response" JSON data into Ember Data store from here...?
});

编写自定义 DS.Adapter 是最好的(或唯一好的)解决方案吗?还是有其他好的、可维护的方法来完成这个?

【问题讨论】:

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


    【解决方案1】:

    最好的方法是编写自己的适配器,但如果您只需要加载模型(不保存),您可以使用适配器的 load 方法 - 像这样:

    socket.on('single-model', function(response) {
        DS.defaultStore.load(response);
    });
    
    socket.on('multiple-models', function(response) {
        for(i in response){
            DS.defaultStore.load(response[i]);
        }
    });
    

    将适配器定义为FixtureAdaptor

    【讨论】:

      【解决方案2】:

      为了记录,我最终选择了这里提出的答案:

      https://stackoverflow.com/a/14508316/1470194

      使用 Ember.Instrumentation 技术,我能够方便地调用我的控制器并以这种方式访问​​我的商店。

      我意识到在许多情况下可能需要编写自定义适配器,但这超出了我项目所需的范围,因此这个解决方案最终是完美的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-22
        • 2013-01-12
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多