将此重新归类为 bug。
我在插件 API 代码 app/assets/javascripts/discourse/app/lib/plugin-api.js 中添加了一个 console.log,以便在调用 modifyClass 时进行记录。
我已移除所有外部插件,以确保没有冲突。
重现步骤:
-
在
stable上创建一个空的论坛(没有 Ember CLI)。在 tests-passed(没有 Ember CLI)上此功能也不起作用。我没有用 Ember CLI 测试过。 -
添加一个主题组件,并在 Common - Head 中添加以下内容:
#1 有效
<script type="text/discourse-plugin" version="0.1">
api.modifyClass('model:user', {
pluginId: 'test-tc',
testFunction: function() {
return 1;
}
});
</script>
-
加载主页
-
控制台显示
modifyClass called for model:user _application-08d9058ddd37ba80992f770509f4919ad0738a17f14fb85167b1dc1f32f8b56e.js:23490:16 Object { pluginId: "test-tc", testFunction: testFunction() } -
在控制台中输入
Discourse.currentUser.testFunction() -
打印出 “1”
#2 无效
- 转到某个主题,例如“Welcome to Discourse”,然后重新加载页面
- 控制台显示相同的“modifyClass called”日志
- 在控制台中输入
Discourse.currentUser.testFunction() - 打印出
Uncaught TypeError: Discourse.currentUser.testFunction is not a function
#3 无效并带警告
- 在主题组件的顶部添加单行,使其看起来像这样:
<script type="text/discourse-plugin" version="0.1">
const userModel = api.container.lookup("model:user");
api.modifyClass('model:user', {
pluginId: 'test-tc',
testFunction: function() {
return 1;
}
});
</script>
- 转到某个主题,例如“Welcome to Discourse”,然后重新加载页面
- 控制台显示相同的“modifyClass called”日志
- 控制台显示警告
“model:user” was already cached in the container. Changes won't be applied. - 在控制台中输入
Discourse.currentUser.testFunction() - 打印出
Uncaught TypeError: Discourse.currentUser.testFunction is not a function
#4 有效
- 将 lookup 行移至主题组件的底部,使其看起来像这样:
<script type="text/discourse-plugin" version="0.1">
api.modifyClass('model:user', {
pluginId: 'test-tc',
testFunction: function() {
return 1;
}
});
const userModel = api.container.lookup("model:user");
</script>
- 转到某个主题,例如“Welcome to Discourse”,然后重新加载页面
- 控制台显示相同的“modifyClass called”日志
- 在控制台中输入
Discourse.currentUser.testFunction() - 打印出 “1”
