Ember.js - 调用父模型钩子来加载孩子的数据 [英] Ember.js - Call parent model hook to load data for child

查看:125
本文介绍了Ember.js - 调用父模型钩子来加载孩子的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ember中有一个主/详细视图。如果我直接调用详细页面,则详细页面的模型钩子需要父(主)模型(数据)。调用细节模型钩子 - 获取/调用模型的正确方式是什么,以便细节钩中的modelFor功能起作用。



路由器:

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

主路由:

  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 [];
}
});
}
});

详细路线:

  App.DeviceRoute = Ember.Route.extend({
model:function(args){
//////获得被叫 - 需要数据从主人!!
var model = this.modelFor('devices.index')。findBy('imei',args.imei);
return model;
}
});

感谢您的帮助

解决方案

devices.index 路由不是父路由,它是另一个叶子路由。通常,您将在资源路线上定义模型,然后通过叶子路径进行访问:

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

主路由:

  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 [];
}
});
}
});

索引路线:(在未来的ember版本中,这将自动获取父级模型)

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

详细路线:

  App.DevicesDeviceRoute = Ember.Route.extend({
model:function(args){
var model = this.modelFor('devices')。findBy('imei' args.imei);
返回模型;
}
});


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

解决方案

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;
    }
});

这篇关于Ember.js - 调用父模型钩子来加载孩子的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆