短:
既然你问了一个余烬应用我奉献一些时间来提供可接受的答案这里是工作jsbin。
长:
我在这里添加一些代码,完整的代码,请看看提供的jsbin。
的index.html
<script type="text/x-handlebars">
Status:
{{#if App.isOffline}}
<span class="offline">Offline</span>
{{else}}
<span class="online">Online</span>
{{/if}}
<hr>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
<h2>Hello World</h2>
</script>
注:我已经使用了JS LIB heyoffline.js,因为它是最好的一个海事组织左右。 我也覆盖了显示模式窗口的函数(lib显示默认情况下发生脱机时的模式窗口,但由于我们要通过我们的应用程序应用程序来控制它,所以不需要),只需将它删除原型overiddes。
app.js
// overrides to not show the default modal window, to get it back just remove this overrides
Heyoffline.prototype.showMessage = function() {
//this.createElements();
if (this.options.onOnline) {
return this.options.onOnline.call(this);
}
};
Heyoffline.prototype.hideMessage = function(event) {
if (event) {
event.preventDefault();
}
//this.destroyElements();
if (this.options.onOffline) {
return this.options.onOffline.call(this);
}
};
//ember app
var App = Ember.Application.create({
isOffline: false,
service: null,
ready: function(){
this.set('service', App.HeyOffline.create());
}
});
// Heyoffline wrapper
App.HeyOffline = Ember.Object.extend({
service: null,
init: function(){
// heyoffline instantiation
this.set('service', new Heyoffline());
// hook into the two important events
this.set('service.options.onOnline', this.offline);
this.set('service.options.onOffline', this.online);
},
online: function(){
App.set('isOffline', false);
console.log("online");
},
offline: function(){
App.set('isOffline', true);
console.log("offline");
}
});
App.ApplicationRoute = Ember.Route.extend({});
为了测试它的工作原理,加载jsbin和离线去,看看状态模板的变化,回去网上看到它再次发生变化。
有了这个设置在地方,你应该得到的浏览器余烬方式的在线/离线状态,享受:)
希望它可以帮助
宾果!谢谢。 – wintermeyer 2013-05-21 07:45:06
Gern geschehen(不客气):) – intuitivepixel 2013-05-21 07:45:37