EmberJS 路由器加载子状态
2018-01-04 13:14 更新
描述
Ember.js提供了一个加载过程来实现加载子状态。如果路由器promise失败,Ember.js会找到一个适当的加载路由,可以转换到子模板立即解决。
加载事件
如果承诺没有立即解决,加载事件将触发默认路由应用程序模板,以克服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>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将上面的代码保存在routing_load_subst.html文件中
在浏览器中打开此HTML文件。
以上内容是否对您有帮助:
更多建议: