lastdance: (Default)
lastdance ([personal profile] lastdance) wrote in [community profile] style_system2009-05-05 09:26 pm

Calling functions in other classes

I've been reading S2 documentation like crazy, and I'm pretty sure the answer is 'no' but I'm going to throw out the question anyway:

Is there any possible way to call functions from a different view than the one you're currently on? For example, the EntryPage class has a function print_comments. But what if I want to print the subjects of the 5 latest comments on the Recent view (maybe in the sidebar)? The RecentPage class doesn't include that function, so I'm assuming there's no way to call it unless the core was rewritten to add it into RecentPage and allow it to take parameters so it knows which comments to pull back. Am I correct in that assumption?

I'll gladly take the question elsewhere if this isn't the right place. Any direction would be so much appreciated.
adalger: Earthrise as seen from the moon, captured on camera by the crew of Apollo 16 (Default)

[personal profile] adalger 2009-05-06 07:01 am (UTC)(link)
This is the right place to ask. :)

You're pretty much correct here. You'd have to do some fancy coding in the RecentPage class to have it retrieve comment subjects for each entry on the page. The EntryPage class is only concerned with a single entry, so it's pretty clear what comments it's looking for, but the RecentPage class isn't built to display any comments, so it's not designed to even care about them beyond how many there are.

It's an interesting idea. Do you mean the 5 most recent comments per entry, or the 5 most recent comments on any entry on the page? The latter would be a little trickier, because you'd have to find probably the five most recent comments on each entry, then sort for which of all those are the five absolute most recent.
zorkfox: A computer screen from the Heart of Gold spaceship in The Hitchhiker's Guide to the Galaxy; it says, Normality Restored. (Six)

Flimsy Suggestion

[personal profile] zorkfox 2009-05-06 08:03 am (UTC)(link)
Perhaps you can access the comments using the Entries[] member of the RecentPage class, on the theory that each element of that array will have data about the comments on that entry. I tried to write some code for you, but my brain hasn't been in code-mode for a long time and I was stymied.

Look through the Entries[] array of your current instance of the RecentPage class and get the titles of the comments from the Comments[] array in those entries until you have five of them. Build in a flow control statement to stop after five (so you don't search every entry needlessly) and also one to handle an instance of no comments (this covers instances where there are no entries yet, or a page full of entries that don't have any comments).

When I glanced at the language reference for S2, I didn't see a simple way to do this because there isn't an until loop or even a while. You have to make up some kind of bastardized foreach. :( Maybe you can write an if statement that calls return when it finds it has collected five comments.

You might be able to use this function from Haven as a template. It's not the greatest example of code, but I hope it'll get your brain working in the right direction:

function RecentPage::lay_sidebar_view_summary() : string
{
    var string return;
    foreach var Entry e ($.entries) {
        var string subject = $e.subject != "" ? $e.subject : $*text_nosubject;
        $return = $return + "→";
        if ($e.security) { $return = $return + " $e.security_icon"; }
        $return = $e.poster.username != $.journal.username ?
                  $return + """ $e.poster:<br /><a href="#entry_$e.itemid">$subject</a><br />""" : 
                  $return + """ <a href="#entry_$e.itemid">$subject</a><br />""";
    }
}
zorkfox: An order ticket from a restaurant with one item written in blue ink: "Something better than this..." (Three)

Re: Flimsy Suggestion

[personal profile] zorkfox 2009-05-06 06:08 pm (UTC)(link)
Aw, heck. I'm sorry; you're totally right about that. It's because I started writing my reply with EntryPage in mind instead of ReplyPage, noticed half-way through, and made corrections. I can only blame the lateness of the hour. ^_^;
zorkfox: An order ticket from a restaurant with one item written in blue ink: "Something better than this..." (Three)

Re: Flimsy Suggestion

[personal profile] zorkfox 2009-05-06 06:46 pm (UTC)(link)
Sigh! I know why they limit the data in cases like this, but it sure is frustrating when you come up with a clever idea, but can't implement it because the computer time becomes a problem. :(
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2009-05-06 04:06 pm (UTC)(link)
No, afraid it's not possible. The problem is that the individual comment data isn't available, unless you are on the entry page. (Completely separate in the backend, so that even if the S2 language syntax were to allow it, there wouldn't be any data to print out).
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2009-05-06 05:49 pm (UTC)(link)
Aha. We do have the number of comments available for the current page. My guess is that that's how they're doing the sidebar (based on code for the page summary, but with modified text).

However, that only shows the number of comments made on the entry, not the time the last comment was made, or the actual comment metadata/text.

If my guess is correct, and they're doing it with pure s2, then the more accurate description of the information is "most popular posts on the front page/from the first 20 entries", though "of the past hour" probably works given the popularity of the community.
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2009-05-06 06:52 pm (UTC)(link)
Hmm, probably not. If they are using a third-party API, and it's not entirely S2, then no amount of documentation on our end will make it clear, I'm afraid!
av8rmike: Neo from Matrix, text: I know S2 (I know s2)

[personal profile] av8rmike 2009-05-07 09:26 pm (UTC)(link)
They're definitely using some kind of off-site tracker in PHP. You can see calls to 0x0 images in the page source (tables and un-validated HTML, ewww).
adalger: Earthrise as seen from the moon, captured on camera by the crew of Apollo 16 (Default)

[personal profile] adalger 2009-05-07 02:28 am (UTC)(link)
I'm confused on this. Is it possible to extend class definitions, or define new classes, in a top-level layer?
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2009-05-07 02:37 am (UTC)(link)
It's possible to define new classes, but not possible to extend class definitions.

(though you can make a new class that extends an existing class).

But I'm afraid I don't see the connection with my comment, and I'm worried that I'm giving the wrong impression of what is/is not possible, somehow.
adalger: Earthrise as seen from the moon, captured on camera by the crew of Apollo 16 (Default)

[personal profile] adalger 2009-05-07 02:48 am (UTC)(link)
Okay. Is there any way to, say, extend the RecentPage class and then have that extended class be used in your layout in place of the pre-defined RecentPage class?
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2009-05-07 02:58 am (UTC)(link)
Hmm, yes. But that doesn't solve the original problem, because you don't have the data available, unless you are visiting an actual entry.
adalger: Earthrise as seen from the moon, captured on camera by the crew of Apollo 16 (Default)

[personal profile] adalger 2009-05-07 03:05 am (UTC)(link)
So, somewhere back in the coding, something only serves the data that fills the Comment[] array if the page passes the isa EntryPage test?
adalger: Earthrise as seen from the moon, captured on camera by the crew of Apollo 16 (Default)

[personal profile] adalger 2009-05-07 03:13 am (UTC)(link)
kk. Now I understand. Now all I need is a coolhunter to say "This would be cool to have changed." ;)