Ember.js提供了一個加載過程來實現(xiàn)加載子狀態(tài)。如果路由器promise失敗,Ember.js會找到一個適當(dāng)?shù)募虞d路由,可以轉(zhuǎn)換到子模板立即解決。
如果承諾沒有立即解決,加載事件將觸發(fā)默認(rèn)路由應(yīng)用程序模板,以克服promise的失敗。
Ember.Route.extend({ //put your model actions: { loading: function(transition, originalRoute){ return true; } } });
<!DOCTYPE html> <html> <head> <title>Emberjs loading Substates</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars" data-template-name="index"> {{#each item in content}} <!-- displaying the items of an array --> <h2>{{item}}</h2> {{/each}} </script> <script type="text/x-handlebars" data-template-name="loading"> <!-- this is the message for the laoding event --> <h1>Loading Contents Please Wait... </h1> </script> <script type="text/javascript"> App = Ember.Application.create(); App.IndexRoute = Ember.Route.extend({ model: function(){ return new Promise(function(resolve, reject){ //setting up the time out for data display in mili sec window.setTimeout(function(){ data = ['Mack', 'Micky', 'Manu']; //promises the data resolve(data); //halts for 2000 milisec }, 2000); }); }, actions: { loading: function(transition, originalRoute){ // Return true to bubble this event to 'loading' or 'indexRoute' return true; } } }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在routing_load_subst.html文件中
在瀏覽器中打開此HTML文件。
更多建議: