【问题标题】:Sails.js stream keys and data to AngularSails.js 将密钥和数据流式传输到 Angular
【发布时间】:2018-01-18 00:14:25
【问题描述】:

我有一个角度前端,它将数据从一个sails 后端(使用sails-mysql 适配器)填充到一个ui-grid 中。该数据集很大,加载需要一段时间才能使页面看起来包含任何数据。

我已经掌握了从帆启动和运行流式传输的基础知识:

findAllStream: function (req, res) {
    Model.stream().pipe(res);
}

到目前为止,这会将每个单独的数据从模型流式传输到前端。我正在使用 angular-oboe 来消费这个流:

vm.myData = [];
var obj = {};
var count = 0;
Oboe({
    url: '/api/model/findAllStream',
    pattern: '*',
    start: function(stream) {
        // handle to the stream
        vm.stream = stream;
        vm.status = 'started';
    },
    done: function(parsedJSON) {
        vm.status = 'done';
    }
}).then(function() {
    // promise is resolved
    console.log('done');
}, function(error) {
    // handle errors
    console.log('error');
}, function(node) {
    // node received
    switch (count) {
        case 0:
            obj['id'] = node;
            break;
        case 1:
            regObj['property'] = node;
            break;
        case 2:
            regObj['function'] = node;
            break;
    }

    if (++count === 3) {
        vm.myData.push(regObj);
        count = 0;
        obj = {};
    }
});

本质上,数据的节点似乎以相同的顺序出现,所以我只是跟踪我收到了多少节点并以这种方式构建对象。然后,我将每个对象推入一个数组中,以便在 uigrid 中使用。

例如,find() 返回的数据是:

[
{
  id: 1,
  property: foo,
  function: bar
},
{
  id: 2,
  property: stuff,
  function: things
}
]

这适用于 angular 和 uigrid。但是通过流我会得到:

1
foo
bar
2
stuff
things

这并不理想,尤其是在模型发生变化的情况下。有没有办法发送密钥而不仅仅是值?或者也许从流中发送 JSON?比如:

  id: 1,
  property: foo,
  function: bar
  id: 2,
  property: stuff,
  function: things

谢谢!

【问题讨论】:

    标签: javascript angularjs node.js sails.js node-streams


    【解决方案1】:

    原来我使用双簧管不正确。我将匹配模式更改为“{id}”,一切都很好。

    Oboe({
        url: '/api/model/findAllStream',
        pattern: '{id}',
        start: function(stream) {
            // handle to the stream
            vm.stream = stream;
            vm.status = 'started';
        },
        done: function(parsedJSON) {
            vm.status = 'done';
        }
    }).then(function() {
        // promise is resolved
        console.log('done');
    }, function(error) {
        // handle errors
        console.log('error');
    }, function(node) {
        // node received
        vm.gridOptions.data.push(node);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 2015-02-07
      • 2021-08-01
      • 1970-01-01
      相关资源
      最近更新 更多