樂帝當年學習backbone時。最開始是看官網todoMVC的實現。後來瞭解到requireJS便於管理JS代碼。就對官網代碼作了requireJS管理。但此時樂帝感受此時的todoMVC仍然不夠簡明,爲了加深對MVC架構的理解。樂帝對原有appview代碼進行了重構,將相關顯示模塊單獨提取出自稱view。實現view原子化。樂帝已經將這個項目上傳(下載地址)。javascript
增長requireJS的文件夾結構:html
這裏主要用到templates用於放置view相應的模板。views則相應backbone中view文件。假設說backbone是前端MVC,那麼model是對數據創建模型。collection則是對model統一管理,view則起到控制器的做用,用於填充數據到模板,並渲染模板到顯示。model、collection起到M做用。view起到C的做用,模板則起到V的做用。前端
而後咱們看一下todoMVC的效果圖:java
從終於效果圖。咱們可以分析出,要對原有appview中解耦出原子view。就需要推斷出哪些是原子view。原子view需要具有兩點:jquery
define([ 'jquery', 'underscore', 'backbone', 'text!templates/toggleAll.html' ], function($, _, Backbone, toggleTemplate) { var ToggleAllView = Backbone.View.extend({ toggleTemplate: _.template(toggleTemplate), events: { "click #toggle-all": "toggleAllComplete" }, initialize: function() { this.listenTo(this.collection, "all", this.render); //除了todoview與todomodel一一相應 // 其它相關操做都會監聽collection }, render: function() { this.$el.html(this.toggleTemplate()); var done = this.collection.done().length; var remaining = this.collection.remaining().length; this.allCheckbox = this.$("#toggle-all")[0]; this.allCheckbox.checked = !remaining; return this; }, toggleAllComplete: function() { var done = this.allCheckbox.checked; this.collection.each(function(todo) { todo.save({ done: done }); }); //這裏經過推斷單選框是否選中。改動所有modeldone屬性 } }); return ToggleAllView; });
initialize: function() { this.listenTo(this.model, "change", this.render); this.listenTo(this.model, "destroy", this.remove); //當模型被刪除,視圖對應被移除 }
initialize: function() { this.listenTo(this.collection, "all", this.render); //除了todoview與todomodel一一相應 // 其它相關操做都會監聽collection }
initialize: function() { // 初始化加入各類視圖,新建視圖並加入到父視圖指定位置 this.footer = this.$el.find('footer'); this.main = $('#main'); this.todoCollection = new todos; inputview = new InputView({ collection: this.todoCollection }); $("#todoapp").prepend(inputview.render().el); //加入輸入框 var toggleAllview = new ToggleAllView({ collection: this.todoCollection }); this.main.prepend(toggleAllview.render().el); //取得數據後,再初始化 this.allCheckbox = this.$("#toggle-all")[0]; this.listenTo(this.todoCollection, "add", this.addOne); this.listenTo(this.todoCollection, "reset", this.addAll); this.listenTo(this.todoCollection, "all", this.render); // 需要數據的視圖。在獲取數據後定義 this.todoCollection.fetch(); // 狀態視圖 statusview = new StatusView({ collection: this.todoCollection }); this.footer.append(statusview.render().el); //取得數據後,再初始化 }, render: function() { // 由於設置了all監聽所有collection的操做。故加入一個項就會被渲染一次,這保證了有修改都會獲得渲染到頁面 var done = this.todoCollection.done().length; var remaining = this.todoCollection.remaining().length; this.allCheckbox = this.$("#toggle-all")[0]; if (this.todoCollection.length) { //渲染時運行顯示或隱藏的代碼 this.main.show(); this.footer.show(); this.footer.html(); //假設collection爲空的話,則清空footer } else { this.main.hide(); this.footer.hide(); } }, // 實現整體顯示