SailsJS - Angular2 - Resourceful Pub / Sub

时间:2017-02-18 09:00:28

标签: javascript angular sails.js

在网络上看不到涵盖Angular2和SailsJS Resourceful Pub / Sub场景的任何内容。我正在尝试观看新记录的模型并在我的Angular2应用程序中显示它们。我正在使用angular2-sails模块。下面的代码成功将数据导入我的角度应用程序,但需要页面刷新才能获取新数据。目标是在不刷新页面的情况下实时获取数据。

Sails Controller Actions:

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

Angular2组件:

import os, sys, urllib2, urllib, zipfile, arcpy
from arcpy import env

path = os.getcwd()

def pickData():
    myCount = 1
    path1 = 'path2URL'
    response = urllib2.urlopen(path1)
    print "Enter the name of the files you need"
    numZips = raw_input()
    numZips2 = numZips.split(",")
    myResponse(myCount, path1, response, numZips2)

def myResponse(myCount, path1, response, numZips2):
    myPath = os.getcwd()
    for each in response:
        eachNew = each.split("  ")
        eachCounty = eachNew[9].strip("\n").strip("\r")
        try:
            myCountyDir = os.mkdir(os.path.expanduser(myPath+ "\\counties" + "\\" + eachCounty))
        except:
            pass
        myRetrieveDir = myPath+"\\counties" + "\\" + eachCounty
        os.chdir(myRetrieveDir)
        myCount+=1
        response1 = urllib2.urlopen(path1 + eachNew[9])
        for all1 in response1:
            allNew = all1.split(",")
            allFinal = allNew[0].split(" ")
            allFinal1 = allFinal[len(allFinal)-1].strip(" ").strip("\n").strip("\r")
            numZipsIter = 0
            path8 = path1 + eachNew[9][0:len(eachNew[9])-2] +"/"+ allFinal1
            downZip = eachNew[9][0:len(eachNew[9])-2]+".zip"
            while(numZipsIter <len(numZips2)):
                if (numZips2[numZipsIter][0:3].strip(" ") == "NWI") and ("remap" not in allFinal1):
                    numZips2New = numZips2[numZipsIter].split("_")
                    if (numZips2New[0].strip(" ") in allFinal1 and numZips2New[1] != "remap" and numZips2New[2].strip(" ") in allFinal1) and (allFinal1[-3:]=="ZIP" or allFinal1[-3:]=="zip"):
                        urllib.urlretrieve (path8,  allFinal1)
                        zip1 = zipfile.ZipFile(myRetrieveDir +"\\" + allFinal1)
                        zip1.extractall(myRetrieveDir)
                #maybe just have numzips2 (raw input) as the values before the county number
                #numZips2[numZipsIter][0:-7].strip(" ") in allFinal1 or numZips2[numZipsIter][0:-7].strip(" ").lower() in allFinal1) and (allFinal1[-3:]=="ZIP" or allFinal1[-3:]=="zip"
                elif (numZips2[numZipsIter].strip(" ") in allFinal1 or numZips2[numZipsIter].strip(" ").lower() in allFinal1) and (allFinal1[-3:]=="ZIP" or allFinal1[-3:]=="zip"):
                    urllib.urlretrieve (path8,  allFinal1)
                    zip1 = zipfile.ZipFile(myRetrieveDir +"\\" + allFinal1)
                    zip1.extractall(myRetrieveDir)
                numZipsIter+=1



pickData()

#client picks shapefiles to add to map
#section for geoprocessing operations




# get the data frames



#add new data frame, title
#check spaces in ftp crawler



os.chdir(path)
env.workspace = path+ "\\symbology\\"
zp1 = os.listdir(path + "\\counties\\")

def myGeoprocessing(layer1, layer2):
    #the code in this function is used for geoprocessing operations
    #it returns whatever output is generated from the tools used in the map
    try:
        arcpy.Clip_analysis(path + "\\symbology\\Stream_order.shp", layer1, path + "\\counties\\" + layer2 + "\\Streams.shp")
    except:
        pass
    streams = arcpy.mapping.Layer(path + "\\counties\\" + layer2 + "\\Streams.shp")
    arcpy.ApplySymbologyFromLayer_management(streams, path+ '\\symbology\\streams.lyr')
    return streams

def makeMap():
    #original wetlands layers need to be entered as NWI_line or NWI_poly
    print "Enter the layer or layers you wish to include in the map"
    myInput = raw_input();
    counter1 = 1
    for each in zp1:
        print each
        print path
        zp2 = os.listdir(path + "\\counties\\" + each)
        for eachNew in zp2:
            #print eachNew
            if (eachNew[-4:] == ".shp") and ((myInput in eachNew[0:-7] or myInput.lower() in eachNew[0:-7])or((eachNew[8:12] == "poly" or eachNew[8:12]=='line') and eachNew[8:12] in myInput)):
                print eachNew[0:-7]
                theMap = arcpy.mapping.MapDocument(path +'\\map.mxd')
                df1 = arcpy.mapping.ListDataFrames(theMap,"*")[0]
                #this is where we add our layers
                layer1 = arcpy.mapping.Layer(path + "\\counties\\" + each + "\\" + eachNew)
                if(eachNew[7:11] == "poly" or eachNew[7:11] =="line"):
                    arcpy.ApplySymbologyFromLayer_management(layer1, path + '\\symbology\\' +myInput+'.lyr')
                else:
                    arcpy.ApplySymbologyFromLayer_management(layer1, path + '\\symbology\\' +eachNew[0:-7]+'.lyr')

                # Assign legend variable for map
                legend = arcpy.mapping.ListLayoutElements(theMap, "LEGEND_ELEMENT", "Legend")[0]
                # add wetland layer to map
                legend.autoAdd = True
                try:
                    arcpy.mapping.AddLayer(df1, layer1,"AUTO_ARRANGE")
                    #geoprocessing steps
                    streams = myGeoprocessing(layer1, each)
                    # more geoprocessing options, add the layers to map and assign if they should appear in legend
                    legend.autoAdd = True
                    arcpy.mapping.AddLayer(df1, streams,"TOP")

                    df1.extent = layer1.getExtent(True)

                    arcpy.mapping.ExportToJPEG(theMap, path + "\\counties\\" + each + "\\map.jpg")
                    # Save map document to path
                    theMap.saveACopy(path + "\\counties\\" + each + "\\map.mxd")
                    del theMap

                    print "done with map " + str(counter1)
                except:
                    print "issue with map or already exists"
                counter1+=1

makeMap() 

1 个答案:

答案 0 :(得分: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") }
  )