From: Tom Dale <sproutc...@tomdale.net>
Date: Wed, 31 Mar 2010 09:32:33 -0700
Local: Wed, Mar 31 2010 12:32 pm
Subject: Re: [sproutcore] Replacing Subviews - Absolutely Lost
Please don't be embarrassed, as this was difficult for me when starting SproutCore as well. I'd like to tackle this problem by discussing two broad concepts, so please bear with me. The first is that we must differentiate between instances and classes. I presume that in the JavaScript that defines your custom views, you are using the extend or design methods to describe the UI. For example, your code probably looks something like: }); That gives us a class for the view, not an actual instance of it. To use this custom view in the view hierarchy, we have to create an instance of it. Now you might be thinking: "Instantiating and managing all of those views sounds like a pain in the ass." And you would be right if you had to do it yourself. Fortunately, there exists the elegant (if strangely named) SC.Page class. In a nutshell, an instance of SC.Page will dynamically instantiate views as they are needed, deferring the CPU and memory hit of instantiation until the very last moment possible. Here's how it does it: The next time you call get(), it will return the same instance that it created the first time, so you're always dealing with the same object. Pretty neat, huh? Now, the next concept to understand: SC.ContainerView. As you've already discovered, SC.ContainerView has a property called nowShowing. When nowShowing changes, the ContainerView will replace its child view with whatever you pass in. nowShowing can be either an SC.View or a string. Setting an SC.View is straightforward but boring; I presume the reason it didn't work for you was that it was probably handed a class instead of an instance. More interestingly, you can also set nowShowing to a string. ContainerView will automatically evaluate that string when it changes. It supports your standard property path, so you can pass something like 'MyApp.viewsController.customView' and it will convert that to an object at runtime. A quick aside about SC.Page: Any views instantiated from a Page have a property called page. Therefore, any view can reference the page by which it was created. ContainerView leverages this by allowing you to set the nowShowing property to the name of another view that belongs to the same page. What I like to do in my apps is define my mainPane in my mainPage. Usually that mainPane will have at least one container view. I then define any views I want to swap out as properties of the mainPage. Here's an example from a Twitter client I'm planning to release as sample code: MyApp.mainPage = SC.Page.design({ contentView: SC.ContainerView.design({ loadingView: SC.View.design({ spinner: SC.ImageView.design({ label: SC.LabelView.design({ value: '_Loading Timeline…', }); If I set the nowShowing property of mainPage.mainPane.contentView to the string "loadingView", it will ask its page object for the property loadingView, see that it is a class, instantiate it, and replace its contents with the contents of the loadingView. Anyway, this can be tricky because it is non-obvious, but once you figure it out I think you'll see that it's really quite powerful stuff! Hope that helped. -Tom On Mar 31, 2010, at 7:40 AM, BinaryPalindrome wrote: > It would seem to be quite basic, and I have to admit to some
> embarrassment at my confusion, but I'm struggling to figure out how to > work with replacing sub/child views. The tutorials I've been able to > find do a great job of detailing how one goes about creating an > effectively static page, with predefined views and child views, but > I've not been able to find anything that speaks to my difficulty. > Let's say that I want to have my application start up with a basic > Depending upon what a user clicks on, there are a number of different > I'm lost at loading a view as a child of an existing view based on a > After a number of dead ends and apparent mistakes, I set up a main > So how does one go about replacing/showing a new child view? > -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||