0
使用引擎設計公司的骨幹創建了一個web應用程序後,我想知道是否應該將「if/then」邏輯從html模板中移出。如果/然後將邏輯移入視圖或模型中或保留在模板中?
爲了明確我的意思,下面是我目前在生產中使用的兩個示例。
如果我將if/then邏輯移出模板,我會將它移動到視圖中,但我不確定這是否是「正確」方式或「主幹」方式。
我在做糟糕的設計決定,還是我做得很好?
謝謝!
簡單實施例1:
在視圖:
//m is the model used by the view
return Backbone.View.extend({
template: _.template(tmpl, null, { variable: 'm' }),
在模板:
{% if(m.title) { %}
<h4> {%- m.title %} </h4>
{% } else { %}
<h4>Experiment Title Goes Here</h4>
{% } %}
復實施例2:
在視圖:
//args are the model attributes passed into the view
initialize: function (args) {
this.currentEngine = args.currentEngine;
this.engineDisplay = args.engineDisplay;
this.engineType = args.engineType;
this.isCurrent = this.model.isCurrent(this.currentEngine);
},
render: function() {
this.$el.html(this.template({
engineDisplay: this.engineDisplay,
engineType: this.engineType,
isCurrent: this.isCurrent;
}));
在模板:
{% if(!engineDisplay) { %}
{% if (m.isCurrent && (engineType === 'GAS' || engineType === 'ECO')) { %}
<span>Not Available</span>
{% } else { %}
<span>
<span>Click here to select</span>
</span>
{% } %}
{% } %}
好的謝謝。所以在模板中使用if/then邏輯是好的嗎? – SkyeBoniwell
@ 999cm999是的。沒關係 –