2011-07-01 74 views
0

我有三层对象,每个对象都是一对多的。我想,当选择不同的笔记本时,页面和列视图元素会得到级联更新。Sproutcore嵌套数组一对多绑定

笔记本电脑>页>列

随着notebooksController和notebookController我可以绑定

App.Notebook = SC.Record.extend({ 
    name: SC.Record.attr(String), 
    pages: SC.Record.toMany('App.Page', {isMaster: YES, inverse: 'notebook'}) 
}); 

App.Page = SC.Record.extend({ 
    pageNumber: SC.Record.attr(Number), 
    notebook: SC.Record.toOne('App.Notebook', {isMaster: NO, inverse: 'pages'}), 
    columns: SC.Record.toMany('App.Column', {isMaster: YES, inverse: 'page'}) 
}); 

App.Column = SC.Record.extend({ 
    columnNumber: SC.Record.attr(Number), 
    page: SC.record.toOne('App.Page', {isMaster: NO, inverse: 'columns'}) 
}); 

在此之后,我似乎无法获取内容结合为pagesController工作。我想要将pagesController,pageController,columnsController和columnController的内容级联下来,这样当用户单击不同的笔记本时,所呈现的视图会自动弹出到正确的内容。

ArrayController notebooksController 
// contents filled from fixture 
ObjectController notebookController 
// bound to notebooksController selection 
ArrayController pagesController 
// contentBinding: 'notebookController.pages' does not work! 
ObjectController pageController 
// bound to pagesController selection 
// and down to column 
+0

如果有关于如何执行这个文件,我很乐意看到它 - 我认为一个orderedObjects绑定可能是一个答案,但似乎并不奏效。 – jasonk

+0

你有一系列笔记本,或只有一个? – hvgotcodes

回答

1

假设你有一个笔记本电脑,尝试

App.notebookController = SC.ObjectController.create({ 
    // call App.notebookController.set('content', aNotebook) 
    // to set the content on this controller 
}); 

App.pageController = SC.ArrayController.create({ 
    // the notebookController is a proxy to the its content, so you dont need 
    // 'content' in the binding contentBinding: 'App.notebookController.pages' 

    // bind the list view to App.pagesController.arrangedObjects. If you look in the code 
    // arranged objects is a reference to the array controller itself, which has array methods 
    // on it 
}); 

App.pageSelectionController = SC.ObjectController.create({ 
    // You need to add 
    // 
    // selectionBinding: 'App.pageSelectionController.content 
    // 
    // to the collection view where you select the page 

    // you can do this in places to see when things change. This controller is just a proxy 
    // to the selected page. 
    selectionDidChange: function(){ 
     console.log('page selection changed to [%@]'.fmt(this.get('content'); 
    }.observes('content') 
}); 

App.columnsController = SC.ArrayController.create({ 
    contentBinding: 'App.pageSelectionController.columns' 

    // again, where you want to show the columns, bind to 
    // App.columnsController.arrangedObjects 
}); 
+0

是的,我相信这也会起作用。在设置观察者之后,我发现装置是我的问题,并且在传递的页面数组中发现“0”长度。感谢您的答复。 – jasonk

+0

@jasonk,你的控制器结构是什么样的? – hvgotcodes

+0

'Autofocus.notebooksController = SC.ArrayController.create({ allowsMultipleSelection:NO, });' 'Autofocus.notebookController = SC.ObjectController.create({ \t contentBinding:SC.Binding.single('自动对焦。 notebooksController.selection'), });' 'Autofocus.pagesController = SC.ArrayController.create({ allowsMultipleSelection:NO, contentBinding: 'Autofocus.notebookController.pages' });' – jasonk

0

我找到了答案。 原来你确实可以简单地使用App.notebookController.pages。

问题是,我的灯具设置不正确。将孩子映射到父母是不够的,父母必须映射回孩子。

我本来有:

Autofocus.Notebook.FIXTURES = [ 
    { guid: 1, title: "Home Notebook", columnCount: 2}, 
    { guid: 2, title: "Work Notebook", columnCount: 2} 
]; 

当我做了以下内容是所有固定:

Autofocus.Notebook.FIXTURES = [ 
    { guid: 1, title: "Home Notebook", columnCount: 2, pages: [11, 12] }, 
    { guid: 2, title: "Work Notebook", columnCount: 2, pages: [21, 22]} 
]; 
+0

小除了什么@jasonk说:当定义夹具记录的'多'侧时,一定要在单个值周围加入[],因为Sproutcore是exp影响一系列值。 (请参阅http://tomhuda-guides.strobeapp.com/fixtures.html) – rajkamal