Think.js開發筆記

大力美少女發表於2019-04-11

基本原理

遵循MVC結構,使用模板語法。Controller呼叫Model中查詢資料庫的方法,通過display函式把資料注入View,View通過模版語法解析資料

  • View層
{% if event.length==0 %}
<li>
	<div class="nodata">暫無資料</div>
</li>
{%- endif %}
{% for i in event -%}
<li>
	<span class="td e-1">{{i.time|default('- -',true)}}</span>
	<span class="td e-2">{{i.country}}</span>
	<span class="td e-3">{{i.city|default('- -',true)}}</span>
	<span class="td e-4">
		{% for j in range(0,5) -%} 
		    {% if loop.index <=i.importance %} 
		         <img src="images/download/x1.png" >
			{% else %}
		    	<img src="images/download/x2.png" > 
			{% endif %}
		{%- endfor %}
	</span>
	<span class="td e-5">
		<span>{{i.event}}</span>
	</span>
</li>
{% endfor %}
複製程式碼
  • controller
   let date=this.get("id");
   let data=[],data2=[],data3=[],time="",count={},lastDay={};
   if(util.strDateTime(date)){
     data2 = await this._calendar.get_event(date);
   }
   this.assign('event', data2);
   return this.display("calendar");
複製程式碼
  • Modal
module.exports = class extends think.Model {
        //查詢財經大事件
    async get_event(date, star, country) {
        //jujin8_economic_event
        var condition2 = "";
        if (!date) {
            condition2 = "DATE(date) = CURRENT_DATE";
        }
        else {
            condition2 = "DATE(date) = DATE('" + date + "')";
        }
        if (star) {
            condition2 += " AND importance>=" + star;
        }
        if (country) {
            condition2 += " AND country=" + country;
        }
        var sql = "SELECT country,time,city,importance,event FROM economic_event " +
            "WHERE  " + condition2 + " ORDER BY created_at,country;";
        var result = await this.query(sql);
        return formatJson(result);
    }
複製程式碼

}

相關文章