使用afterModel或重定向鉤子來(lái)獲取有關(guān)重定向的當(dāng)前模型的信息。它們接收兩個(gè)參數(shù),即分解模型和轉(zhuǎn)換。
Ember.Route.extend({ afterModel: function(posts, transition) { this.transitionTo('routeName', posts.get('firstObject')); } });
<!DOCTYPE html> <html> <head> <title>Emberjs After the Model is Known</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="application"> {{outlet}} </script> <script type="text/x-handlebars" data-template-name="index"> <a href="#" {{action "getIds"}}>Click To Get IDs Of Student</a> </script> <script type="text/x-handlebars" data-template-name="posts"> <b>Student Id:</b> <i>{{stud_id}} </i><br/> </script> <script type="text/javascript"> App = Ember.Application.create({}); App.Router.map(function() { this.route('posts', { path: 'school/:stud_id'}); }); App.IndexController = Ember.Controller.extend({ actions: { getIds: function() { this.transitionToRoute('posts', { stud_id: 1 }); } } }); App.IndexRoute = Ember.Route.extend({ afterModel: function(params) { return params; } }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在routing_aftr_mod.html文件中
在瀏覽器中打開(kāi)此HTML文件。
更多建議: