Ember 车把助手中的换行符?
Newline in Ember handlebars helper?
对于如何在我的特定情况下使用这个助手,我仍然有点模糊。我从对象数组中拉出一个随机对象,并在单击按钮时显示它们。每个对象都有 3 个属性:word、language、trans。渲染对象时,我真的希望它显示如下
字
语言
翻译
我在渲染时将其居中,但全部在一行中。这是我的助手的代码:
Ember.Handlebars.helper('randomize', function(myArray, options) {
var random = myArray[Math.floor(Math.random() * myArray.length)];
return random.word + random.language + random.trans;
});
所以我的愚蠢问题是如何在呈现时包含换行符?
我认为一个更好的工具是一个组件。组件会自动附带一个模板来完全按照您的意愿执行操作,然后您的显示逻辑将与您的业务逻辑分开。
App.RandomWordComponent = Ember.Component.extend({
word: null,
language: null,
translation: null,
createRandomWord: function() {
this.set('word', Math.random());
this.set('language', Math.random());
this.set('translation', Math.random());
}.on('didInsertElement')
});
和模板:
<p style="font-weight: bold">
Word: {{word}}
</p>
<p>
language: {{language}}
</p>
<p>
translation: {{translation}}
</p>
然后在您的其他模板中使用它:
{{random-word}}
我对其进行了一些简化,因此您需要根据自己的需要对其进行调整,但希望这能为您指明正确的方向。 complete example here
对于如何在我的特定情况下使用这个助手,我仍然有点模糊。我从对象数组中拉出一个随机对象,并在单击按钮时显示它们。每个对象都有 3 个属性:word、language、trans。渲染对象时,我真的希望它显示如下
字
语言
翻译
我在渲染时将其居中,但全部在一行中。这是我的助手的代码:
Ember.Handlebars.helper('randomize', function(myArray, options) {
var random = myArray[Math.floor(Math.random() * myArray.length)];
return random.word + random.language + random.trans;
});
所以我的愚蠢问题是如何在呈现时包含换行符?
我认为一个更好的工具是一个组件。组件会自动附带一个模板来完全按照您的意愿执行操作,然后您的显示逻辑将与您的业务逻辑分开。
App.RandomWordComponent = Ember.Component.extend({
word: null,
language: null,
translation: null,
createRandomWord: function() {
this.set('word', Math.random());
this.set('language', Math.random());
this.set('translation', Math.random());
}.on('didInsertElement')
});
和模板:
<p style="font-weight: bold">
Word: {{word}}
</p>
<p>
language: {{language}}
</p>
<p>
translation: {{translation}}
</p>
然后在您的其他模板中使用它:
{{random-word}}
我对其进行了一些简化,因此您需要根据自己的需要对其进行调整,但希望这能为您指明正确的方向。 complete example here