Ngắn:
Kể từ khi bạn yêu cầu một ứng dụng ember Tôi đã dành một thời gian để cung cấp một câu trả lời chấp nhận được. Đây là số jsbin đang hoạt động.
Các dài:
tôi thêm ở đây một số mã, cho mã hoàn chỉnh hãy nhìn vào jsbin cung cấp.
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>
Lưu ý: Tôi đã sử dụng js lib heyoffline.js vì nó là một trong những tốt nhất xung quanh IMO. Tôi cũng đã overriden các chức năng hiển thị một cửa sổ phương thức (hiển thị của lib cho mỗi cửa sổ mặc định khi ngoại tuyến xảy ra, nhưng vì chúng ta sẽ kiểm soát nó trough ứng dụng ember của chúng tôi nó không cần thiết), để lấy lại nó chỉ cần loại bỏ nguyên mẫu 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({});
Để kiểm tra rằng nó hoạt động, nạp jsbin và đi offline, xem làm thế nào Trạng thái trong mẫu thay đổi, trở lại trực tuyến để xem nó thay đổi một lần nữa.
Với thiết lập này ở nơi bạn sẽ nhận được trực tuyến/tình trạng ẩn của trình duyệt cách ember, thưởng thức :)
Hy vọng nó giúp
Bingo! Cảm ơn bạn. – wintermeyer
Gern geschehen (bạn được chào đón) :) – intuitivepixel