2017-08-09 86 views
1

我有一個角度前端,它將來自sails後端的數據(使用sails-mysql適配器)填充到UI網格中。此數據集很大,需要一段時間才能在頁面顯示包含任何數據之前加載。Sails.js將鍵和數據流傳輸到Angular

我從帆流啓動和運行的基礎知識:

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

到目前爲止,這個流從模型到前端數據的每一個單件。我使用的角度,雙簧管來使用這個流:

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中使用。

例如,將由發現返回的數據()將是:

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

這個偉大的工程用角和uigrid。但與流我只會得到:

1 
foo 
bar 
2 
stuff 
things 

這並不理想,特別是如果模型更改。有沒有辦法發送密鑰,而不僅僅是價值?或者也許從流中發送JSON?例如:

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

謝謝!

回答

2

原來我錯誤地使用了Oboe。我將匹配模式更改爲「{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); 
}); 
相關問題