在 Seaside 中动态生成 JavaScript 的简单方法是什么?

发布于 2024-10-14 02:57:42 字数 300 浏览 3 评论 0原文

我正在尝试将 Disqus 添加到我正在撰写的博客中。为了处理 Seaside 会话,我需要向 JS 添加唯一的 discus_identifier 或 disqus_url。我重写了组件的 #script 方法,但它只能返回字符串文字。

我看到两个选项:

  1. 动态生成 JS,将其保存到文件中,然后将该文件加载到我的组件中。
  2. 为每个博客条目添加永久链接。

有更简单的方法吗?或者这些方法中的一种(或两种)很容易做到吗?我是 Smalltalk 和 Seaside 的新手,不确定如何完成这两件事。

I'm trying to add Disqus to a blog I'm writing. To deal with Seaside sessions I need to either add a unique discus_identifier or disqus_url to the JS. I overrode my component's #script method but it can only return a string literal.

I see two options:

  1. Dynamically generate the JS, save it to a file, and load that file in to my component.
  2. Add a permalink for each blog entry.

Is there an easier way? Or is one (or both) of these ways easy to do? I'm new to Smalltalk and Seaside and am unsure how to accomplish either of these two things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2024-10-21 02:57:42

是的,有一个更简单的方法。您可以直接在#script方法中生成正确的Discus JS代码。它应该返回一个字符串文字,但您可以动态创建此字符串。例如通过使用 WriteStream。

您的博客条目还需要一个永久链接。您可以使用#initialRequest:方法来处理这些永久链接。

Yes there is an easier way. You can generate the correct Discus JS code directly in the #script method. It should return a String literal but you can create this String on the fly. For example by using a WriteStream.

You blog entries also need a permalink. You can use the #initialRequest: method to handle these permalinks.

苦笑流年记忆 2024-10-21 02:57:42

动态 javascript 的东西

如果我是对的,那么转发按钮之类的东西也会出现这种情况。 (这是我手头上可以向您提供的示例)。

我在博客中所做的是一个名为 BITRetweet 的小型专用海边组件,您可以使用永久链接(以及用户名和样式首选项)对其进行配置。忘记文件的东西(这只会让事情变得复杂),一切都是动态的。它呈现如下:

BITRetweet>>renderContentOn: html

html script with: self customizedJavascript.
html script url: self buttonJavascriptSource.

BITRetweet>>customizedJavascript

| script |

script := JSScript new.

script add: (('"',self permalink,'"') asJSObject assignTo: 'tweetmeme_url').

isCompact ifTrue:[
    script add: ('"compact"' asJSObject assignTo: 'tweetmeme_style')].

script add: (('"',username,'"') asJSObject assignTo: 'tweetmeme_source').
script add: (('"',shortener,'"') asJSObject assignTo: 'tweetmeme_service').

^ script 

BITRetweet>>>buttonJavascriptSource

"Answers the url to the source of the script for the button.
See: 
http://help.tweetmeme.com/2009/04/06/tweetmeme-button/"

^ 'http://tweetmeme.com/i/scripts/button.js'

最后对 String 进行一些修改,如下所示:

String>>asJSObject

^ JSObject new alias: self

工作对于

永久链接部分,有两件事:

  1. 使用它生成它
  2. (使应用程序在收到请求时做出反应)

对于 1,您可以执行以下操作:


PostComponent>>updateUrl: anUrl

super updateUrl: anUrl.

anUrl addToPath: model asURLNice

Post>>asURLNice

"Answers the receiver in an (destructive) encoded 
way which is url friendly"

^ String streamContents: [:stream|
    self do:[:char|
        char isSeparator 
            ifTrue:[stream nextPut: $-]
            ifFalse:[
                char isAlphaNumeric ifTrue:[
                    stream nextPut: char asLowercase asNonDiacritical]]]]

对于 2,您必须在主应用程序组件中执行类似的操作

BlogApplication>>initialRequest: aRequestOrNil

| paths |

super initialRequest: aRequestOrNil.

aRequestOrNil ifNil:[^ nil].

(aRequestOrNil url asString endsWith: '/sitemap.xml') ifTrue:[
    ^ self respondSitemap].

paths := aRequestOrNil url path.
paths size < 2 ifTrue:[^nil].

(Post atURLTitle: paths last) ifNotNilDo: [:value | 
    ^ self readPost:  value].

实时示例,

您可以在 中看到所有实际操作我的博客选择性创造力
我需要 asNonDiaritic,因为我的博客是三种语言的,但是如果您需要的话,可以在 squeaksource 中找到 DiariticSupport,如果您需要它,

请享受黑客攻击的乐趣

/

dynamic javascript stuff

if I'm right, that's the kind of scenario you'll get for things like retweet buttons too. (which is the thing I have at hand to provide you examples of).

What I did in my blog is a little specialized seaside component named BITRetweet that you configure with the permalink (and username and style preference). Forget the files stuff (that will only complicate things), everything is on the fly. It renders with this:

BITRetweet>>renderContentOn: html

html script with: self customizedJavascript.
html script url: self buttonJavascriptSource.

BITRetweet>>customizedJavascript

| script |

script := JSScript new.

script add: (('"',self permalink,'"') asJSObject assignTo: 'tweetmeme_url').

isCompact ifTrue:[
    script add: ('"compact"' asJSObject assignTo: 'tweetmeme_style')].

script add: (('"',username,'"') asJSObject assignTo: 'tweetmeme_source').
script add: (('"',shortener,'"') asJSObject assignTo: 'tweetmeme_service').

^ script 

BITRetweet>>>buttonJavascriptSource

"Answers the url to the source of the script for the button.
See: 
http://help.tweetmeme.com/2009/04/06/tweetmeme-button/"

^ 'http://tweetmeme.com/i/scripts/button.js'

and finally a little hack for String, like this:

String>>asJSObject

^ JSObject new alias: self

working with permalinks

for the permalink part, there are two things:

  1. generating it
  2. using it (making the app to react when the request comes with it)

for 1 you can do something like this:


PostComponent>>updateUrl: anUrl

super updateUrl: anUrl.

anUrl addToPath: model asURLNice

Post>>asURLNice

"Answers the receiver in an (destructive) encoded 
way which is url friendly"

^ String streamContents: [:stream|
    self do:[:char|
        char isSeparator 
            ifTrue:[stream nextPut: $-]
            ifFalse:[
                char isAlphaNumeric ifTrue:[
                    stream nextPut: char asLowercase asNonDiacritical]]]]

and for 2 you have to do something like this in your main application component:

BlogApplication>>initialRequest: aRequestOrNil

| paths |

super initialRequest: aRequestOrNil.

aRequestOrNil ifNil:[^ nil].

(aRequestOrNil url asString endsWith: '/sitemap.xml') ifTrue:[
    ^ self respondSitemap].

paths := aRequestOrNil url path.
paths size < 2 ifTrue:[^nil].

(Post atURLTitle: paths last) ifNotNilDo: [:value | 
    ^ self readPost:  value].

live examples

you can see all that in action in my blog or in selective creativity
the asNonDiacritical is needed for me because I blog trilingual but the DiacriticalSupport is available in squeaksource if you need it

have fun hacking

o/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文