1

我刚开始使用 EmberJS 并尝试定义导航结构。

我想要一个也有子页面的页面对象。我将如何在数据结构中关联它们?

这是我到目前为止所拥有的:



        App.Page = DS.Model.extend({
        title:          attr('string',                      { defaultValue:''}),
        url:            attr('string',                      { defaultValue:''}),
        defaultPage:    attr('boolean',                     { defaultValue:false}),
        parentPage:     attr('string',                      { defaultValue:''}),
        children:       attr('string',                      { defaultValue:''}),
        position:       attr('string',                      { defaultValue:'left'}),

        getChildren:function(){
            return App.NavigationController.filterProperty('parentPage',this.get('title'));
        }
    });

到目前为止,这是我使用它的方式:




       this.pushObject(App.Page.createRecord({
            title:          "Page",
            url:            "/page"
        }));


        this.pushObject(App.Page.createRecord({
            title:          "Child of Page",
            url:            "/child_of_page",
            parentPage:     "Page"
        }));

任何指针?

编辑示例:

我正在尝试您的建议,但收到以下信息:

pPg = App.Page.createRecord({title:'Page1'});

pPg.get('childen').pushObject(App.Page.createRecord({title:'cPage1', parentPage:pPg}));
// output TypeError: Cannot call method 'pushObject' of undefined

var cPg1 = App.Page.createRecord({title:'cPage1', parentPage:pPg});
var cPg2 = App.Page.createRecord({title:'cPage2', parentPage:pPg});

pPg.get('children').objectAt(0);
// outputs undefined
pPg.get('children').objectAt(1);
// outputs undefined
4

1 回答 1

1

我认为,就像您与其他模型有关:使用belongsToand hasMany

App.Page = DS.Model.extend({
    title:          attr('string',                      { defaultValue:''}),
    url:            attr('string',                      { defaultValue:''}),
    defaultPage:    attr('boolean',                     { defaultValue:false}),
    parentPage:     DS.belongsTo('App.Page'),
    children:       DS.hasMany('App.Page'),
    position:       attr('string',                      { defaultValue:'left'})
});

不再需要该getChildren方法,您只需使用childrenandparentPage属性:

var parentPage = App.Page.createRecord({
    title:          "Page",
    url:            "/page"
});
parentPage.get('children').pushObject(App.Page.createRecord({
    title:          "Child of Page",
    url:            "/child_of_page"
}));

var anotherChild = App.Page.createRecord({
    title:          "Child of Page",
    url:            "/child_of_page",
    parentPage:     parentPage
});

这将创建一个Page有 2 个孩子的对象。

创建对象时有两种定义关系的方法 - 要么告诉父母关于孩子的事情,要么告诉孩子关于父母的事情,不需要告诉两者 - ember 将同步关系。这仅适用于您定义不同模型之间的关系!

编辑

使用getwithobjectAt访问hasMany关系:

  • parentObj.get('children')- 得到所有的孩子。请注意,这不是一个常规的 JS 数组,而是一个Ember.Array因此没有[ ]运算符。
  • parentObj.get('children').objectAt(1)- 获得特定的孩子。

编辑 2

当您处理与同一模型的关系时,您需要告诉父母和孩子他们的关系:

var parent = App.Page.createRecord({
    title:          "Page",
    url:            "/page"
});
parent.get('children').pushObject(App.Page.createRecord({
    title:      "Child of Page",
    url:        "/child_of_page",
    parentPage: parent
}));

请参阅this fiddle with working example。

于 2013-02-10T14:06:03.933 回答