- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我知道这个问题,Display online and offline (e.g. airplane) mode in an Ember.js App但这并不能解决我遇到的所有问题。这个问题的答案使用 Heyoffline.js,这是一个很棒的库(JSbin 使用的是它),但它不会检查 Ember 应用程序“启动”时的连接状态,只有当我关闭 wifi 时使用该应用程序。我正在创建一个离线/在线兼容的 Ember 应用程序,我只需要知道启动时的状态,我需要完全控制“胆量”,即 UI、 View 等......Heyoffline.js 做的太多了给你。
简单地说,我需要启动一个 Ember 应用程序:
window.App = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_VIEW_LOOKUPS: true,
service: null,
isOffline: function() {
var networkCheck = navigator.onLine ? true : false;
return networkCheck;
},
ready: function() {
this.set('service', App.HeyOffline.create());
Ember.debug("App ready!");
}
});
App.HeyOffline = Ember.Object.extend({
service: null,
init: function() {
this.set('service', new Heyoffline({
noStyles: true,
prefix: "network",
text: {
title: "No Internet Connection",
content: "Your experience will be limited, it's suggested that you connect to the internet",
button: "Continue Anyway."
}
}));
this.set('service.options.onOnline', this.offline);
this.set('service.options.onOffline', this.online);
},
online: function() {
App.set('isOffline', false);
Ember.debug('Online');
},
offline: function() {
App.set('isOffline', true);
Ember.debug('Offline');
}
});
App.ApplicationRoute = Ember.Route.extend();
View .js
App.NetworkView = Ember.View.extend({
layoutName: "_network_error",
classNames: ['network-error', 'flash-message'],
classNameBindings: ['isHidden:hide:show'],
isHidden: true,
networkFail: function() {
var self = this;
console.log("Network Failure");
this.set('isHidden', false);
Ember.run.next(function() {
setTimeout(function(){
self.set('isHidden', true);
}, 3000);
});
},
didInsertElement: function() {
var self = this;
Ember.run.next(function() {
if(App.isOffline) { // <--- THIS ALWAYS RETURNS TRUE, doh
self.networkFail();
}
});
}
});
应用程序.hbs
{{outlet}}
{{outlet modal_dialog}}
{{outlet modal_status}}
{{outlet debug}}
{{#view App.NetworkView }} {{/view }}
我的应用程序远不止于此,但我想我已经提取了所有相关部分。我并不是说必须这样做,我唯一的要求是:
任何想分享的想法、建议或建议将不胜感激。
最佳答案
一个问题是:
if(App.isOffline) {//<--- 这总是返回真,doh
您应该将其作为一个函数来调用,而不仅仅是访问它。一个函数是一个真值,但你想要的是函数的结果。
现在,对于其余的功能。将它封装在服务上并使其可供其余 Controller / View 使用是有意义的。为简单起见,我将其作为 ApplicationController 的一部分。
App.ApplicationController = Ember.Controller.extend( {
isOnline: true, // assume we're online until proven wrong
init: function () {
updateNetworkStatus();
},
updateNetworkStatus: function () {
var appController = this;
if (!navigator.onLine) {
this.set('isOnline', false);
return; // return early, no point in pinging the server if we have no LAN
}
Ember.$.get('myserver.com/status').done(function () {
// todo: consider checking the result
appController.set('isOnline', true);
}).fail(function () {
appController.set('isOnline', false);
}).always(function () {
Ember.run.later(appController, 'updateNetworkStatus', 60000);
});
}
});
我们仍然检查 navigator.online,但如果它是真的,我们不假设我们一直连接到服务器。我们检查一下。一旦我们得到响应,我们就会更新标志并每分钟更新一次该标志。
从其他 Controller ,我们可以简单地添加对应用程序 Controller 的依赖。
App.OtherController = Ember.Controller.extend( {
needs: ['application'],
});
然后在属性中使用它或在执行某些操作之前用作检查:
App.OtherController = Ember.Controller.extend( {
needs: ['application'],
canEdit: function () {
return this.get('controllers.application.isOnline');
}.property('controllers.application.isOnline')
});
或者从你的 Angular 来看
{{controllers.application.isOnline}}
希望这对您有所帮助。正如我所说,我通常会将其封装到一个 service
中,然后可以将其注入(inject) Controller ,只是为了将此类问题从应用程序中移开,但这可能需要回答第二个问题。
希望对你有帮助
关于javascript - 启动和监听器时的 Ember js 网络连接检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122570/
当单击复选框(或选择所有复选框)时,我想向 CheckboxSelectionModel 添加一个监听器。 var smSensors = new xg.CheckboxSelectionModel(
我有一个简单的程序,允许 2 个客户端连接到服务器。 连接后,他们可以轮流点击空白卡片图像。 一旦 2 个客户中的任何一个点击空白卡片图片,卡片图片将变为 Ace 俱乐部图片。 更改将显示在客户端的两
我在这里看到了一个代码,该代码以字符串的形式检索鼠标的当前图标,但是此代码使用了TTimer来实现。 因此,我想知道是否存在某些事件(侦听器)来检测鼠标光标图标上的这些更改。 下面是使用TTimer的
我想在我的配置对象上获得一个 onload 事件。 以下工作,除非我创建一个 config.listeners={..} (我认为这就是我需要的?)替换 this.onload({...}); 我什至
通常,在 Java 中,当我有一个向其他对象提供某种通知的对象时,我将使用 Listener/Observer 模式。 有没有更类似于 Scala 的方式来做到这一点?我应该在 Scala 中使用这种
我有一个带有动画器的游戏对象和一些可以触发事件的动画(具有特定的回调函数)。 只要我将脚本添加到与动画器相同的游戏对象(包括接收器),一切都会正常工作: public class AnimatorEv
我有一个带有监听器的 DialogFragment,用于单击按钮以调用 fragment 中的函数。 我收到 lateinit property listener has not been initi
这个问题已经有答案了: Java ActionListener error: incompatible types (4 个回答) 已关闭 5 年前。 我最近刚刚开始学习 Java 代码。我在添加监听
我的代码遇到问题。我想知道是否有一种更简单的方法来使用监听器,而不是不断地这样做: example.addActionListener(new java.awt.event.ActionListene
有没有办法使用 .net 创建控制台应用程序。或通过某个端口监听 SMTP 消息的服务? 我需要创建一个中间层对象来捕获和处理 smtp 消息。也就是说,我希望该监听器发送和接收 smtp 消息,然后
我有一个带有动画器的游戏对象和一些可以触发事件的动画(具有特定的回调函数)。 只要我将脚本添加到与动画器相同的游戏对象(包括接收器),一切都会正常工作: public class AnimatorEv
我有许多向主事件生成服务注册的监听器。然而,我想告诉听众,事件流在某个时刻将会结束。您会通过简单地调用监听器上的方法(例如 finish())来完成此操作,还是有一个单独的事件方法 streamFin
我的代码有什么问题。 我创建了一个 JList,添加了项目并将其推到左侧(BorderLayout.WEST)。每次单击列表项时,我希望在列表右侧显示一个面板。但问题是,当选择列表项并运行监听器时,到
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
这可能是一个简单的问题,但我没有看到它。 我有一个界面 public interface RenderableListener{ public void update(T element);
有人可以直接指出我的正确方向吗?当从组合框中选择适当的选项时,我希望小程序中的 Action 监听器显示从 html 文件检索的 jlabel 中的 3 个参数之一。 干杯 最佳答案 对于组合框,您需
我有一个网站,每个页面上都有许多 jQuery 事件处理程序,所有这些都在一个大型 .js 文件中。 这意味着对于任何页面,大多数事件处理程序都是针对不存在且根本不会使用的 HTML。 这会影响我的表
我有一些 jQuery 监听器设置,用于监听 type="text" 字段上的表单输入。但是,当用户从自动完成下拉框中选择一个选项(即他们之前输入的值已被记住以供将来使用)时,下面的监听器不会收集该值
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我正在用 unity (c#) 做一个类似国际象棋的游戏,但我在尝试进行向上转换以将信息从一个 child 发送到另一个抽象类时遇到了困难。 基本上,我有一个抽象类,它有一个带有函数的事件/委托(de
我是一名优秀的程序员,十分优秀!