在为一家引擎设计公司创建了一个使用主干的web应用程序之后,我想知道是否应该将“if /然后”逻辑从html模板中移出。
为了澄清我的意思,这里有两个我目前在生产中使用的例子。
如果我将If /然后的逻辑从模板中移出,我会将其移到视图中,但我不确定这是“正确的”方式还是“主干”方式。
我是做了糟糕的设计决策,还是做得还不错?
谢谢!
简单示例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>
{% } %}
{% } %}发布于 2015-12-02 16:50:51
我认为你的第一次实施是正确的。不要把逻辑放在视野之外。“正确的”方法,或者说“主干”方法,是将逻辑保持在需要的地方:
我肯定我错过了东西..。我会等到评论告诉我我有多错,然后我会纠正它。
-Sheers
https://stackoverflow.com/questions/34047236
复制相似问题