Search code examples
sproutcore

Binding Sproutcore Controllers to Models


So I am read the Sproutcore tutorials in the past days. A lot of them talk about how to bind views and controllers. Then there are tutorials on the model that talk about the store and records. What I am missing is a best practice/tutorial on how to link controllers to models.

Given I have a model:

Myapp.User = SC.Record.extend({
    id: SC.Record.attr(Number),
    name: SC.Record.attr(String)
});

And a controller:

Myapp.controller = SC.ArrayController.create(
 {
       allowsMultipleSelection: NO,
        selection: null,
        isEditable: YES
});

What is the best way to bind them? (I know I can query the store and set Myapp.controller.set('content', store.find(Myapp.User)), but I guess there's a better way to bind them together?

Sorry, if this is really a newbie question, I would also appreciate a link to a manual or guide that answers this. I was googling for hours and did not find something useful (and easy enough for me to understand).


Solution

  • The general process that you will want to use is to have your statechart set the content on the controller at the appropriate time.

    For instance, when your user is in the loggedOut state, you probably don't need most of your controllers to have data. So, when entering the loggedIn state (using the enterState method), you probably want to setup all of your controllers and then setup your UI. You'll generally set the content of the controller to either a specific object (like the user object you get back from your authorization system), or a MyApp.store.find(...) call which returns a SC.RecordArray.