2017-02-18 75 views
1

沒有在網上看到很多關於Angular2和SailsJS Resourceful Pub/Sub場景的東西。我正在嘗試觀看新記錄的模型並將它們顯示在我的Angular2應用程序中。我正在使用angular2-sails模塊。下面的代碼成功地將數據導入我的角度應用程序,但需要刷新頁面以獲取新數據。我們的目標是在不刷新頁面的情況下實時獲取數據。SailsJS - Angular2 - 足智多謀的Pub/Sub

帆控制器操作:

newCall: function(req, res) { 

    if (!req.isSocket) 
     return res.badRequest; 

    Call.create({ 
     payload: req.body 
    }) 
    .exec(function (err, newCall){ 
     if (err) return res.negotiate(err); 
     Call.publishCreate(newCall); 
     res.ok(newCall); 
    }); 
    }, 

getCalls: function(req, res) { 
    Call.find() 
    .then(function(calls) { 
     Call.watch(req.socket); 
     console.log(req.socket.id + ' is now watching for changes to Calls model'); 
     res.send(calls); 
    }) 
    .catch(function(err) { 
     console.log(err); 
    }); 
} 

Angular2組件:

ngOnInit() { 
    let sails = this._sailsService; 
    sails.connect("http://localhost:1337") 
    sails.get('/socket/getCalls') 
    .subscribe(
     (resData) => { this.calls = resData.data.length}, 
     (error) => { console.log("oooops, error occured") }, 
    () => { console.log("we are finished") } 
    ) 
    } 

回答

0

索性就.on("model"),我.publishCreate荷蘭國際集團

sails.on("call") 
    .subscribe(
    (resData) => { 
    this.call_count = this.call_count + 1; 
    this.calls_realtime.push(resData.data); 
    }, 
    (error) => { console.log("Error") }, 
() => { console.log("End") } 
) 
相關問題