我正在学习如何为Discourse构建一个插件。我的插件从渲染的角度来看是正常的,但缓冲区创建的按钮似乎不会解决被覆盖的把手模板中按钮的操作。
我注册的JavaScript资源 -
(function() {
"use strict";
Discourse.AssetValuationComponent = Ember.Component.extend({
// Id of the element for tracking
elementId: '1234',
// Classes to apply to the element
// classNames: ['asset-valuation-box'],
actions: {
alerttitle: function (topic) {
console.log(this);
console.log(topic);
}
},
render: function(buffer) {
var topic = this.get('topic');
buffer.push("<div class='asset-valuation-box'>");
buffer.push("<button {{action 'alerttitle' topic }}>Click me!</button>");
buffer.push("</div>");
}
});
})();
我的车把模板 -
{{asset-valuation user=currentUser asset=model}}
它正在渲染按钮,但单击按钮不会触发警报标题操作 - 我想知道是否因为使用渲染是在Ember走过DOM之后。有什么想法吗?
答案 0 :(得分:1)
渲染只是将字符串注入dom。您最好使用templateName
或template
参数来定义模板,如下所示:
App.AssetValuationComponent = Ember.Component.extend({
// Id of the element for tracking
elementId: '1234',
template :Ember.Handlebars.compile("<div class='asset-valuation-box'><button {{action 'alerttitle' topic }}>Click me!</button></div>"),
// Classes to apply to the element
// classNames: ['asset-valuation-box'],
actions: {
alerttitle: function (topic) {
console.log(this);
console.log(topic);
}
}
});