Javascript 信号器更新会中断页面上的布局和脚本
当一个信号器更新进入我们的页面来更新我们的模式时,我们的项目名称会改变,我们的脚本似乎会中断 简要概述:我们的信号器更新发送到网站很好,但数据本身有一个无效的名称。 更新后,我们的项目将刷新为格式错误的名称。首先,我们的名字不应该被signer更新,而且我在代码中似乎找不到任何对它的引用。 关闭模式,我们的Highcharts和Angular脚本会抛出控制台错误 服务器端代码:Javascript 信号器更新会中断页面上的布局和脚本,javascript,asp.net,angularjs,highcharts,signalr,Javascript,Asp.net,Angularjs,Highcharts,Signalr,当一个信号器更新进入我们的页面来更新我们的模式时,我们的项目名称会改变,我们的脚本似乎会中断 简要概述:我们的信号器更新发送到网站很好,但数据本身有一个无效的名称。 更新后,我们的项目将刷新为格式错误的名称。首先,我们的名字不应该被signer更新,而且我在代码中似乎找不到任何对它的引用。 关闭模式,我们的Highcharts和Angular脚本会抛出控制台错误 服务器端代码: public partial class Device { if (device != null) { if
public partial class Device
{
if (device != null)
{
if ((Enumerables.DeviceType)device.Type == Enumerables.DeviceType.Store)
SignalrClient.UpdateStore(device.DeviceID);
else // check if need to update a modal on the dashboard
{
foreach (var key in SignalrClient.DevicesDictionary.Keys)
{
var devices = SignalrClient.DevicesDictionary[key];
if (devices != null)
{
if (devices.Contains(device.DeviceID))
SignalrClient.UpdateModal(key, device.DeviceID);
}
}
}
}
}
class SignalrClient
{
public static async Task Start()
{
if (_hubConnection == null || _hubConnection.State == ConnectionState.Disconnected)
{
_hubConnection = new HubConnection("http://stevessiteofamazingboats.net/");
_dashboardHubProxy = _hubConnection.CreateHubProxy("DashboardHub");
_dashboardHubProxy.On("OnRegisterDevice", new Action<string, int>(OnRegisterDevice));
_dashboardHubProxy.On("OnDeregisterDevices", new Action<string>(OnDeregisterDevices));
_dashboardHubProxy.On("OnDeregisterDevice", new Action<string, int>(OnDeregisterDevice));
await _hubConnection.Start();
}
}
public static async void UpdateModal(string connectionId, int deviceId)
{
await Start();
if (_hubConnection.State == ConnectionState.Connected)
await _dashboardHubProxy.Invoke("UpdateModal", new object[] { connectionId, deviceId });
}
}
public class DashboardHub : Hub
{
private static string EventHubConnectionId {get;set;}
private AlarmDBEntities db = Utils.DbContext;
public void UpdateModal(string connectionId, int deviceId)
{
var db = Utils.DbContext;
var device = db.Device.Find(deviceId);
var modal = new Portal.DeviceModalViewModel()
{
DeviceId = deviceId,
SuctionGroups = device.Device1.Where(x => (Enumerables.DeviceType)x.Type == Enumerables.DeviceType.SuctionGroup).Select(x => new DeviceModalViewModel.SGNode()
{
SubChildren = x.Device1.Where(y => (Enumerables.DeviceType)y.Type == Enumerables.DeviceType.Compressor).Select(y => new DeviceModalViewModel.DeviceNode()
{
DeviceId = y.DeviceID,
Name = y.Name,
Amp = db.Property.Where(z => z.Name == "Amps" && z.DeviceID == y.DeviceID).OrderByDescending(z => z.CreatedOn).Select(z => z.Value).FirstOrDefault()
}).OrderBy(y => y.Name).ToList()
}).OrderBy(x => x.Name).ToList(),
};
}
如果在信号器刷新后尝试打开图表,则会显示Highcharts错误:
store.js:856 Uncaught TypeError: $(...).highcharts is not a function
at Object.success (store.js:856)
at c (<anonymous>:1:132617)
at Object.fireWith [as resolveWith] (<anonymous>:1:133382)
at b (<anonymous>:1:168933)
at XMLHttpRequest.<anonymous> (<anonymous>:1:173769)
主要的问题是更新事件正在破坏某些东西。命名问题的优先级较低,但我确信它是相关的。发现并修复了该问题 我们发现,格式错误的名字总是将真名的前5个字符删掉,所以我在靠近底部的地方修复了它,尽管我仍然不知道这种删减是在哪里发生的 更严重的问题是,脚本的破坏也得到了解决。 在UpdateModel方法中,其中一个脚本正在查找storeID字段以及deviceID字段。在将日志打印到Chrome javascript控制台之后,我可以看到storeID始终返回0,即使它在UpdateModel方法之前已经初始化 我所要做的就是跟随该死的火车,在DeviceId下添加storeID字段:
public void UpdateModal(string connectionId, int deviceId)
{
var db = Utils.DbContext;
var device = db.Device.Find(deviceId);
var modal = new Portal.DeviceModalViewModel()
{
DeviceId = deviceId,
*THIS*-> StoreId = db.Device.Where(x => device.Name.Contains(x.Name.Replace("-Store", "")) && x.ParentID == null).Select(x => x.DeviceID).FirstOrDefault(),
SuctionGroups = device.Device1.Where(x => (Enumerables.DeviceType)x.Type ==
Enumerables.DeviceType.SuctionGroup).Select(x => new
DeviceModalViewModel.SGNode()
{
SubChildren = x.Device1.Where(y => (Enumerables.DeviceType)y.Type ==
Enumerables.DeviceType.Compressor).Select(y => new
DeviceModalViewModel.DeviceNode()
{
DeviceId = y.DeviceID,
*ALSO THIS*-> Name = "12345Comp " + y.Name.Substring(y.Name.Length - 2),
Amp = db.Property.Where(z => z.Name == "Amps" && z.DeviceID == y.DeviceID).OrderByDescending(z => z.CreatedOn).Select(z => z.Value).FirstOrDefault()
}).OrderBy(y => y.Name).ToList()
}).OrderBy(x => x.Name).ToList(),
};
阅读你能裁剪你的代码并制作一个实例吗,例如在jfiddle上?刚刚在添加了JSFiddle,虽然没有C#它不能完全工作,但它应该能让其他人更好地了解正在发生的事情。这里还添加了服务器端信号器代码,以显示更好的细节。
Exception: Sequence contains no elements
Type: System.InvalidOperationException
public void UpdateModal(string connectionId, int deviceId)
{
var db = Utils.DbContext;
var device = db.Device.Find(deviceId);
var modal = new Portal.DeviceModalViewModel()
{
DeviceId = deviceId,
*THIS*-> StoreId = db.Device.Where(x => device.Name.Contains(x.Name.Replace("-Store", "")) && x.ParentID == null).Select(x => x.DeviceID).FirstOrDefault(),
SuctionGroups = device.Device1.Where(x => (Enumerables.DeviceType)x.Type ==
Enumerables.DeviceType.SuctionGroup).Select(x => new
DeviceModalViewModel.SGNode()
{
SubChildren = x.Device1.Where(y => (Enumerables.DeviceType)y.Type ==
Enumerables.DeviceType.Compressor).Select(y => new
DeviceModalViewModel.DeviceNode()
{
DeviceId = y.DeviceID,
*ALSO THIS*-> Name = "12345Comp " + y.Name.Substring(y.Name.Length - 2),
Amp = db.Property.Where(z => z.Name == "Amps" && z.DeviceID == y.DeviceID).OrderByDescending(z => z.CreatedOn).Select(z => z.Value).FirstOrDefault()
}).OrderBy(y => y.Name).ToList()
}).OrderBy(x => x.Name).ToList(),
};