window.AppView = Backbone.View.extend({ // Instead of generating a new element, bind to the existing skeleton of // the App already present in the HTML. el: $("#todoapp"), // Our template for the line of statistics at the bottom of the app. statsTemplate: _.template($('#stats-template').html()), ...
Core.Template = { cache: {}, // pending: {}, // callback-, load: function(url, callback) { callback = callback || function() {}; if (this.cache[url]) { // , callback callback(this.cache[url]); return; } // callback if (this.pending[url]) { this.pending[url].push(callback); return; } this.pending[url] = [callback]; jQuery.ajax({ // url : url, dataType: 'text', complete: function(resp) { var cache = this.cache[url] = _.template(resp.responseText); // _.each(this.pending[url], function(cb) { // cb(cache); }); delete this.pending[url]; // }.bind(this), error: function() { throw new Error("Could not load " + url); } }); } };
Core.View = Backbone.View.extend({ renderQueue: false, // initialize: function() { if (this.templateUrl) { Core.Template.load(this.templateUrl, function(data) { this.template = data; // if (this.renderQueue !== false) { // _.each(this.renderQueue, function(item) { this._render.apply(this, item); }.bind(this)); this.renderQueue = false; } }.bind(this)); } }, _render: function() {}, // render render: function() { if (!this.template) { // - this.renderQueue = this.renderQueue || []; this.renderQueue.push(arguments); return this; } return this._render.apply(this, arguments); // } });
window.AppView = Core.View.extend({ el: $("#todoapp"), // templateUrl: '/templates/app.html', initialize: function() { AppView.__super__.initialize.apply(this); // ... }, // render _render statsTemplate template _render: function() { this.$('#todo-stats').html(this.template({ total: Todos.length, done: Todos.done().length, remaining: Todos.remaining().length })); }, ...
Source: https://habr.com/ru/post/138783/
All Articles