简体   繁体   English

SailsJS - Angular2 - Resourceful Pub / Sub

[英]SailsJS - Angular2 - Resourceful Pub/Sub

Don't see much of anything on the web that covers Angular2 and SailsJS Resourceful Pub/Sub scenarios. 在Web上看不到包含Angular2和SailsJS Resourceful Pub / Sub场景的任何内容。 I am trying to watch a model for new records and display them in my Angular2 app. 我正在尝试观看新记录的模型并在我的Angular2应用程序中显示它们。 I am using the angular2-sails module. 我正在使用angular2-sails模块。 The code below successfully gets the data into my angular app but requires a page refresh to get the new data. 下面的代码成功将数据导入我的角度应用程序,但需要页面刷新才能获取新数据。 The goal is to get the data in real-time without a page refresh. 目标是在不刷新页面的情况下实时获取数据。

Sails Controller Actions: Sails控制器操作:

  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 Component: 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") }
    )
  }

Simply had to .on("model") that I am .publishCreate ing 只需要.on("model")我就是.publishCreate ing

 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") }
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM