Ember.js - Call parent model hook to load data for child

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I have a master/detail view in Ember. If i am calling the detail page directly, the model hook of the detail page needs the model(data) from the parent(master). The detail model hook is called - whats the proper way to get/call the model so the modelFor function in the detail hook works.

Router:

App.Router.map(function(){     this.resource('index', { path: '/' } );     this.resource('devices', { path: '/devices'}, function() {         this.resource('device', { path: ':imei'});     }); });

Master Route:

App.DevicesIndexRoute = Ember.Route.extend({     model: function() {          var self = this;         return requestController.get({             url: "foo/bar/",             success: function(data) {                 console.log(data);                 return data;             },             error: function() {                 console.log("error");                 return [];             }         });     } });

Detail Route:

    App.DeviceRoute = Ember.Route.extend({         model: function(args) { ////// Gets Called - needs data from master!!             var model = this.modelFor('devices.index').findBy('imei', args.imei);             return model;         }     });

Thanks for help

回答1:

The devices.index route is not the parent route, it's another leaf route. Typically you would define the model on the resource route and then access through the leaf routes:

App.Router.map(function(){     this.resource('index', { path: '/' } );     this.resource('devices', { path: '/devices'}, function() {         this.route('device', { path: ':imei'});     }); });

Master route:

App.DevicesRoute = Ember.Route.extend({     model: function() {          var self = this;         return requestController.get({             url: "foo/bar/",             success: function(data) {                 console.log(data);                 return data;             },             error: function() {                 console.log("error");                 return [];             }         });     } });

Index route: (in future ember versions this will automatically pick up parent's model)

App.DevicesIndexRoute = Ember.Route.extend({     model: function() {         this.modelFor('devices');     } });

Detail route:

App.DevicesDeviceRoute = Ember.Route.extend({     model: function(args) {         var model = this.modelFor('devices').findBy('imei', args.imei);         return model;     } });


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!