Skip to content

Instantly share code, notes, and snippets.

@ColinCampbell
Forked from justinpeterman/core.js
Created July 20, 2011 08:23
Show Gist options
  • Save ColinCampbell/1094572 to your computer and use it in GitHub Desktop.
Save ColinCampbell/1094572 to your computer and use it in GitHub Desktop.
Nested Records
Authoring = SC.Application.create(
store: SC.Store.create().from(SC.Record.fixtures)
}) ;
Authoring.LibraryFile = SC.Record.extend({
name: SC.Record.attr(String),
path: SC.Record.attr(String),
size: SC.Record.attr(Number),
type: SC.Record.attr(String),
checksum: SC.Record.attr(String),
cdn: SC.Record.attr(Boolean)
});
Authoring.libraryFileController = SC.ArrayController.create({
// you probably want a chained observer here
// this will fire if `content` or `content.files` changes,
// instead of just `content.files`
contentBinding: 'Authoring.libraryController*content.files'
}) ;
sc_require('models/library_model');
//sc_require('models/file_model'); add here?
Authoring.Library.FIXTURES = [
{
user_id : 1,
name : "Justin",
files : [
{
guid : 0,
name : "photo1.png",
path : "some/path",
size : 26,
type : "image/png",
checksum : "",
cdn : false
},
{
guid : 1,
name : "photo2.png",
path : "some/path",
size : 14,
type : "image/png",
checksum : "",
cdn : false
},
{
guid : 2,
name : "photo3.png",
path : "some/path",
size : 12,
type : "image/png",
checksum : "",
cdn : false
},
{
guid : 3,
name : "photo4.png",
path : "some/path",
size : 4,
type : "image/png",
checksum : "",
cdn : false
},
{
guid : 4,
name : "photo5.png",
path : "some/path",
size : 18,
type : "image/png",
checksum : "",
cdn : false
},
{
guid : 5,
name : "photo6.png",
path : "some/path",
size : 12,
type : "image/png",
checksum : "",
cdn : false
}
]
}
];
Authoring.libraryController = SC.ObjectController.create({
content: null
});
Authoring.Library = SC.Record.extend({
primaryKey: 'user_id',
name: SC.Record.attr(String),
user_id: SC.Record.attr(Number),
files: SC.Record.toMany('Authoring.LibraryFile', { nested: true })
});
SC.ScrollView.design({
layout: { top: 140 },
contentView: SC.ListView.design({
layout: { top: 0 },
classNames: 'library-list',
actOnSelect: YES,
showAlternatingRows: YES,
rowHeight: 24,
contentBinding: "Authoring.libraryFileController",
selectionBinding: "Authoring.libraryFileController.selection",
contentValueKey: "name",
action: "fileClicked"
})
})
// call to statechart action
// Here's the problem:
// If you look at the docs for SC.Store#find: http://docs.sproutcore.com/#doc=SC.Store&method=find&src=false
// You'll see that calling it without an ID parameter creates a RecordArray and returns it
// You're setting this to the content of an SC.ObjectController, which is expecting to proxy an object, not
// an array. So you're binding from Authoring.libraryController (an SC.ObjectController) to Authoring.libraryFileController
// which is an SC.ArrayController (this part is right). I'm suspecting you want to set the content of libraryController
// to an object. If you decide what your primaryKey for library is going to be (I'm assuming user_id but it can be whatever)
// then you just change it to load that ID. It will return an Authoring.Library object and produce the results you want!
Authoring.libraryController.set('content', Authoring.store.find(Authoring.Library, 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment