16. Learn backbone.js tutorial from scratch for beginners(Part 19) Collection to underscore.js templ

C:\Users\Administrator\Desktop\gulp\app.js

jQuery(document).ready(function($) {
  var FirstModel = Backbone.Model.extend({
  	defaults: {
  		name: 'Lionel',
  		team: "Viet Nam",
  		no: 10
  	}
  });
  
  var model1 = new FirstModel({
  	name: 'Lionel 1',
  	team: "Viet Nam",
  	no: 16
  });
  var model2 = new FirstModel({
  	name: 'Lionel 2',
  	team: "Viet Nam",
  	no: 17
  });
  var model3 = new FirstModel({
  	name: 'Lionel 3',
  	team: "Viet Nam",
  	no: 18
  });
  var MyCollection =  Backbone.Collection.extend({
  	model: FirstModel
  });
  var MycollectionObj = new MyCollection([model1, model2, model3]);
  var ModelView = Backbone.View.extend({
  	el: '#div1',
  	template: _.template($("#ourTemplate").html()),
  	collection: MycollectionObj,
  	initialize: function() {
  		this.render();
  	},
  	render: function() {	
  		this.$el.html(this.template({
  			collection: this.collection.toJSON()
  		}));
  	}
  });
  vmodel = new ModelView();
});

C:\Users\Administrator\Desktop\gulp\index.html

jQuery(document).ready(function($) {
  var FirstModel = Backbone.Model.extend({
  	defaults: {
  		name: 'Lionel',
  		team: "Viet Nam",
  		no: 10
  	}
  });
  
  var model1 = new FirstModel({
  	name: 'Lionel 1',
  	team: "Viet Nam",
  	no: 16
  });
  var model2 = new FirstModel({
  	name: 'Lionel 2',
  	team: "Viet Nam",
  	no: 17
  });
  var model3 = new FirstModel({
  	name: 'Lionel 3',
  	team: "Viet Nam",
  	no: 18
  });
  var MyCollection =  Backbone.Collection.extend({
  	model: FirstModel
  });
  var MycollectionObj = new MyCollection([model1, model2, model3]);
  var ModelView = Backbone.View.extend({
  	el: '#div1',
  	template: _.template($("#ourTemplate").html()),
  	collection: MycollectionObj,
  	initialize: function() {
  		this.render();
  	},
  	render: function() {	
  		this.$el.html(this.template({
  			collection: this.collection.toJSON()
  		}));
  	}
  });
  vmodel = new ModelView();
});

Last updated

Navigation

Lionel

@Copyright 2023