我static_url
用来引用位于资源目录中的静态文件。这适用于文字 URL 字符串,但不适用于使用变量构造的复合 URL:
static_url(foo + '/' + bar)
SproutCore 的预处理器会忽略这一点。
那么有什么方法可以使用复合 URL 来引用静态文件吗?或者,由于我使用的是一组有限的 URL,我是否必须先介绍每个复合 URL,然后才能引用它?
我static_url
用来引用位于资源目录中的静态文件。这适用于文字 URL 字符串,但不适用于使用变量构造的复合 URL:
static_url(foo + '/' + bar)
SproutCore 的预处理器会忽略这一点。
那么有什么方法可以使用复合 URL 来引用静态文件吗?或者,由于我使用的是一组有限的 URL,我是否必须先介绍每个复合 URL,然后才能引用它?
此答案假定您使用的是 SC1。
我相信这static_url()
是在编译时由构建工具(sc-build 和 sc-server)处理的,而不是在运行时由框架处理的。
构建工具的代码可以在https://github.com/sproutcore/abbot找到。我相信这base.rb replace_static_url()
是完成工作的地方。
所以下面的代码:
imageView: SC.ImageView.design({
layout: { top: 0, left: 0, width: 200, height: 18 },
value: static_url('picture.png')
})
在交付给浏览器之前被编译成以下内容:
imageView: SC.ImageView.design({
layout: { top: 0, left: 0, width: 200, height: 18 },
value: '/static/vvv/en/current/source/resources/picture.png?1320555999'
})
要在运行时创建复合 URL,请尝试使用绑定。
// In your view
imageView: SC.ImageView.design({
layout: { top: 0, left: 0, width: 200, height: 18 },
valueBinding: 'Vvv.controller.imageValue'
})
// In another file, define the controller
Vvv.controller = SC.Object.create({
foo: '/static/vvv/en/current/source/resources',
bar: 'bug.png',
imageValue: function() {
return this.get('foo') + '/' + this.get('bar');
}.property().cacheable()
});
此外,如果您有一组有限的资源,您可能想尝试一下:
Vvv.controller = SC.Object.create({
foo: static_url('foo.png'),
bar: static_url('bar.png'),
isFoo: false,
imageValue: function() {
if (this.get('isFoo')) {
return this.get('foo');
} else {
return this.get('bar');
}
}.property('isFoo').cacheable()
});
您可以通过设置isFoo
为 true 或 false 在运行时更改图像。
希望这可以帮助。